We have been experiencing some problems in using react now but it kinda boils to one part of how we have been using react.
How should we have been showing/hiding child components?
This is how we have coded it (this are only snippets of our components)...
_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>
)
}
and lately I've been reading examples like it should've been somewhere along this lines:
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>
)
}
Should I have been using that React.render()? It seems to stop various things like shouldComponentUpdate
to cascade to child and things like e.stopPropagation
...
showChild
that you toggle when yourdiv
is clicked. CallingsetState
then rerenders your component.stopPropagation
not working or we need to manually update Child whenshouldComponentUpdate
triggers on parent. But I haven't really seen any documents regarding this "preferred way", only examples of various tutorials. So should we have been using the 2nd solution?this.refs
for that, I just quickly wrote it in jQuery.unable to find component at root ''