I'm still a bit starting up on react and I've now used my 2nd solution (here) to show/hide components.
My problem now is if I have multiple child elements and I need to open another component (Preference) (this I managed already, and clicking outside or other place hides it) but at the same time I need to pass information to that other component. I have attached my small demo of it. My question again is how do I pass from Child
/Child2
components say it's _preferences
or it's coordinates to the Preferences
component?
edit: added jsfiddle link.
the whole code snippet:
<html>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.7.0/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.1/react-with-addons.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.1/JSXTransformer.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<div id="render-here"></div>
<script type="text/jsx">
var Parent = React.createClass({
getInitialState: function () {
return {
showPreference: false,
childComponents: 0
};
},
shouldComponentUpdate: function() {
console.log('parent should update');
return true;
},
_click: function() {
this.setState({childComponents: this.state.childComponents + 1});
},
_showPreference: function() {
this.setState({showPreference: true})
},
_hidePreference: function() {
this.setState({showPreference: false})
},
render: function() {
var childComponents = [];
_.times(this.state.childComponents, function(n) {
if (n % 2)
childComponents.push(<Child key={'foobar'+n} showPreference={this._showPreference} />)
else
childComponents.push(<Child2 key={'foobar'+n} showPreference={this._showPreference} />)
}.bind(this));
return(
<div style={{height:'100%', width:'100%', border:'1px solid blue'}} onClick={this._hidePreference}>
{this.state.showPreference ? <Preference /> : null}
<div onClick={this._click}>Add components</div>
{childComponents}
</div>
)
}
});
var Preference = React.createClass({
shouldComponentUpdate: function() {
console.log('preference should update');
return true;
},
_click: function(e) {
e.stopPropagation();
},
render: function() {
return(
<div ref="pref" style={{position:'absolute', left: '250px'}} onClick={this._click}>
<div>Some preferences here...</div>
</div>
)
}
});
var Child = React.createClass({
shouldComponentUpdate: function() {
console.log('child should update');
return true;
},
_preferences: ['child', 'preferences'],
_click: function(e) {
e.stopPropagation();
this.props.showPreference();
},
render: function() {
return(
<div ref="me" onClick={this._click}>Child - click me for preference</div>
)
}
});
var Child2 = React.createClass({
shouldComponentUpdate: function() {
console.log('child should update');
return true;
},
_preferences: ['child2', 'preferences'],
_click: function(e) {
e.stopPropagation();
this.props.showPreference();
},
render: function() {
return(
<div ref="me" onClick={this._click}>Child 2 - click me for preference</div>
)
}
});
React.render(<Parent name="foobar" />, document.getElementById('render-here'));
</script>
</body>
</html>