我正在通过一个小例子来学习 Facebook React。我决定检查我对this
绑定的了解是否正常,因此我创建了三个React.class
可变状态位于父级的位置,中间仅将回调传递给子级以对其进行操作。
基本结构:
- MainFrame (states here)
- FriendBox (only pass the callbacks for change states to Friend)
-Friend
请注意,我无法使用transferThisProp
,但实际上我更喜欢“手动”制作。
FriendBox 渲染包含以下内容:
var allFriends = this.props.friends.map((function (f) {
return(
<Friend key = {f.id}
name = {f.name}
select = {this.props.select}
/>
)
}).bind(this))
朋友渲染包含以下内容:
return(
<div className="friend">
{this.props.name}
<a href="" onClick={this.props.select(this.props.key)}>
select
</a>
</div>
)
运行我的代码时,我收到以下消息:
MainFrame.sendToFriendH:
Invariant Violation: receiveComponent(...):
Can only update a mounted component. react.js:7276
Uncaught Error: Invariant Violation:
receiveComponent(...): Can only update a mounted component.
有趣的是,当使用 chrome 的 react 扩展时,我可以检查虚拟DOM
是否正常并且绑定是否正常。一切看起来都很好,除了第一个Friend
元素的子组件说_lifeCycleState: "UNMOUNTED"
这让我觉得我犯了一个错误,底部的孩子没有被渲染和安装。所有代码都失败了,但我不知道确切原因。谁能告诉我为什么该元素没有自动安装,我该如何解决?
完整代码:http: //jsfiddle.net/RvjeQ/