我们在使用react now时遇到了一些问题,但这与我们使用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
个子元素...