- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 Meteor 中的 React 还很陌生。
TL;DR:在我的数据容器中,更改 ReactiveVar 的值不会重新呈现我的 View 。
我有这个代码:
import React, { Component, PropTypes } from 'react';
import ReactDOM from 'react-dom';
import { Meteor } from 'meteor/meteor';
import { createContainer } from 'meteor/react-meteor-data';
// Mail composer - interface for generating enews
class MailComposer extends Component {
constructor(props) {
super(props);
}
handleSubmit( event ) {
event.preventDefault();
const text = this.refs.mailText.value.trim(),
title = this.refs.mailTitle.value.trim(),
rcpts = this.refs.mailRecpts.value.trim();
console.log(text, title, rcpts);
if ( text && title && rcpts ) {
// ...
this.props.hasTestedIt.set( true );
} else {
// ...
}
}
componentDidMount () {
console.log(this);
$( this.refs.textArea ).autosize();
}
getBtnText () {
return ( this.props.hasTestedIt.get() ? "Send it" : "Test it" );
}
render() {
let self = this;
Tracker.autorun(function(){
console.log(self.props.hasTestedIt.get());
});
return (
<div className="panel panel-default panel-primary">
<div className="panel-heading">
<h3 className="panel-title">Mail composer</h3>
</div>
<div className="panel-body">
<form className="form-group" onSubmit={this.handleSubmit.bind(this)}>
<div className="input-group">
<span className="input-group-addon" id="basic-addon1">Title</span>
<input type="text" className="form-control" ref="mailTitle" />
</div>
<br/>
<label htmlFor="js-recipients">Recipients:</label>
<select className="form-control" width="width: initial;" id="js-recipients" ref="mailRecpts">
<option>Admins</option>
<option>All</option>
</select>
<br/>
<label htmlFor="comment">Text:</label>
<textarea className="form-control" rows="5" ref="mailText"></textarea>
<br/>
<button className="btn btn-primary" type="submit">
{this.getBtnText()}
</button>
</form>
</div>
</div>
);
}
}
MailComposer.propTypes = {
hasTestedIt: PropTypes.object.isRequired
};
export default createContainer( () => {
return {
hasTestedIt: new ReactiveVar( false )
};
}, MailComposer);`
但是,当我在提交处理程序中设置 ReactiveVar
属性时,按钮中的 getBtnText
方法返回的文本不会更改。我尝试将三元直接放入 HTML 中,但得到了相同的结果。var 已正确设置为 true,因为自动运行正确地记录了更改。
在复制了该组件的另一个组件中,我确实正确地重新渲染了该组件,但在 find
上使用 .fetch()
来映射返回的renderTasks
方法中的数组,用于呈现新组件的列表。
请问我缺少什么?我该如何解决它?
非常感谢!
最佳答案
组件不会更新,因为传递的 hasTestedIt
属性本身没有更改。改变的是它所持有的值(value)。
关注您真正感兴趣的值。
// Declare the reactive source somewhere appropriately.
const hasTestedIt = new ReactiveVar( false );
// Watch its value instead.
export default createContainer( () => {
return {
hasTestedIt: hasTestedIt.get()
};
}, MailComposer);`
请注意,现在它是传递给 MailComposer
的值,而不是可以引用来更新值的 ReactiveVar
。在这种情况下我们如何更新它?
一种最简单的方法是像以前一样通过 hasTestedIt
,尽管这不是我个人的建议。
// Watch its value instead.
export default createContainer( () => {
return {
// Reactive source that triggers re-rendering.
valueOfHasTestedIt: hasTestedIt.get(),
// Provided for the contained component to reference to set new values.
// Leaving this alone doesn't trigger re-rendering!
hasTested
};
}, MailComposer);
在我看来,这并不优雅。另一种方法是将回调函数传递给 MailComposer
,该函数可用于更新值。
const updateHasTestedIt = ( value ) => hasTestedIt.set( value );
// Watch its value instead.
export default createContainer( () => {
return {
hasTestedIt: hasTestedIt.get(),
updateHasTestedIt,
};
}, MailComposer);
class MailComposer extends Component {
...
handleSubmit( event ) {
...
this.props.updateHasTestedIt( true );
...
}
...
};
这次好多了。
可以开发更多,但这实际上取决于您对应用程序的偏好。您且只有您可以做出设计决策。
关于javascript - ReactiveVar 不重新渲染 React 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38906561/
我的父模板包含在子模板中。我需要使用子模板中的父 ReactiveVar。我可以使用 Session 方法,但对于我的要求,Session 方法不起作用。如何从父模板访问 ReactiveVar 值?
什么时候应该使用 Session和 ReactiveVar ?我用 Session变量作为组件之间的通信媒介。让我们以 Stackoverflow 为例。 我标记了三个假设的组件。我们来看看filte
我的主页模板有一个加载功能,该功能非常适合订阅用户集合。不幸的是,加载占用了整个页面,我想在等待用户加载时显示一些一般信息。 我已经在我的创建帐户功能中使用 ReactiveVar 的注册模板实现了这
我在 Meteor 应用中使用 react 变量来根据用户操作显示不同的内容。 代码如下所示: home.js Template.home.onCreated( function() { //
我对 Meteor 中的 React 还很陌生。 TL;DR:在我的数据容器中,更改 ReactiveVar 的值不会重新呈现我的 View 。 我有这个代码: import React, { Com
我知道使用 ReactiveVar 和 Session 变量之间的区别在基本层面上是局部变量与全局变量的问题。 ReactiveDict 就像一个本地 session 对象。 但是,如果您已经在使用
假设我有一个带有输入字段的 templateA.html ,然后是另一个单独的 templateB.html ,它应该显示 输入字段的值templateA.html 因为它们被输入。我已经让它与 Se
我有一个 Meteor 方法可以返回我的应用程序中的所有用户帐户 returnUsers: function(){ return Meteor.users.find().fetch(); }
如果我将 ReactiveVar 存储在 Session 中,然后读取它,我会看到我的 ReactiveVar 被转换为对象。 var v = new ReactiveVar(4) Session.s
我有一个 Meteor ReactiveVar,它用作数据存储中的更新触发器。然而,每次设置 react 变量时,跟踪器都不会运行。 似乎有时当快速连续设置状态时,跟踪器不会运行。 这是我商店的代码:
我有一个包含两部分的模板 View ,一个是表单,然后是一条感谢消息。我想在提交表单时淡入感谢信息。我该怎么做?: {{#if showForm }} some form {{else}}
我在 Meteor.js 中的 Blaze 模板上遇到了一个奇怪的问题。以下模板渲染四次(使用 {{#each}} 运算符) {{name}}
我尝试使用 ReactiveVar。我不知道如何处理 ReactiveVar。这是我试过的代码。 Template.Home.helpers({ names: function(){ te
目前我正在尝试使用功能性无状态组件,因为它们似乎更容易测试/模拟/保持故事书的独立性。 我已经开始使用 withTracker 以便将 React 组件与 Meteor 集成,并且在使用 Meteor
我想在 onRendered 中访问 ReactiveVar 变量时出错传递调用“getUsersData”的结果时出现异常:TypeError:无法读取 null 的属性“userData”
我是一名优秀的程序员,十分优秀!