Please check out a working JSFiddle demo .
var Child = React.createClass({
render: function() {
return (
<div className="chatBar">
<div onClick={this.props.onClick} className="closeBTN">
<img src="http://www.freeiconspng.com/uploads/silver-close-button-png-15.png"/>
</div>
</div>
);
}
});
var ChatBar = React.createClass({
getInitialState: function () {
return { childVisible: false };
},
render: function() {
return(
<div>
<div onClick={this.onToggle} className="chatBTN">
<img src="http://www.omeglechat.eu/wp-content/uploads/2016/10/omegle-mnogochat.png"/>
</div>
{
this.state.childVisible
? <Child onClick={this.onToggle.bind(this)} />
: null
}
</div>
)
},
onToggle: function() {
this.setState({childVisible: !this.state.childVisible});
}
});
React.render(<ChatBar />, document.body);
First use one flag in state:
onToggle: function() {
this.setState({childVisible: !this.state.childVisible});
}
Secondly, in order to call the function ( onClick) in the child component, you need to pass the handler through <Child onClick={this.onToggle.bind(this)} />and call it in the child component throughonClick={this.props.onClick}
source
share