我们已经遇到了一些问题,在使用react过来,但现在它还挺归结到我们是如何被使用的react的一部分。
我们应该如何显示/隐藏子组件?
这就是我们对其进行编码的方式(这只是我们组件的片段)...
_click: function() {
if ($('#add-here').is(':empty'))
React.render(<Child />, $('#add-here')[0]);
else
React.unmountComponentAtNode($('#add-here')[0]);
},
render: function() {
return(
<div>
<div onClick={this._click}>Parent - click me to add child</div>
<div id="add-here"></div>
</div>
)
}
最近我一直在阅读这样的例子,它应该是这样的:
getInitialState: function () {
return { showChild: false };
},
_click: function() {
this.setState({showChild: !this.state.showChild});
},
render: function() {
return(
<div>
<div onClick={this._click}>Parent - click me to add child</div>
{this.state.showChild ? <Child /> : null}
</div>
)
}
我应该使用 React.render() 吗?它似乎阻止了像shouldComponentUpdate
级联到孩子之类的各种事情e.stopPropagation
......