ここであなたがそれを行うことができる方法です。そして、ここで働く例JSBinです:https://jsbin.com/yixano/2/edit?html,js,output
var ModalParent = React.createClass({
getInitialState: function() {
return {showModal: false};
},
toggleShowModal: function() {
this.setState({showModal: !this.state.showModal});
},
render: function() {
return (
<div>
<button type="button" onClick={this.toggleShowModal.bind(this)}>Toggle Show Modal</button>
{this.state.showModal ?
<Modal onModalClose={this.toggleShowModal.bind(this)}/> :
<div></div>}
<h4>State is: </h4>
<pre>{JSON.stringify(this.state, null, 2)}</pre>
</div>);
}
});
var Modal = React.createClass({
render: function(){
return <div><button type="buton" onClick={this.props.onModalClose}>Close</button></div>
}
});
ReactDOM.render(<ModalParent/>, document.getElementById("app"));
は、ここでの考え方は、親の中で状態は子供のアクションに基づいて変更することができるようにモーダルにModalParent上の関数への参照を渡すことです。
ご覧のとおり、子供には "onModalClose"という小道具があり、閉じるボタンをクリックすると呼び出される関数リファレンスが必要です。親では、対応するtoggleShowModalをこのonModalCloseプロパティにバインドします。
これは私が探していた答えです。ありがとうございました! –