私はreact-modal
を使用していますが、その間、アプリケーションのレンダリング中に状態を設定していた問題が発生しました。どのように次のコードは、.bind(this)
が付属して大丈夫です:.bind(this)はsetStateレンダリングの問題を解決するのはなぜですか?
constructor(props) {
super(props);
this.state = {
open: false
}
}
openModal() {
this.setState({
open: true
})
}
render() {
return (
<div>
<button onClick={this.openModal.bind(this)}>Open Modal</button>
<Modal isOpen={this.state.open}>
<h1>I am a modal</h1>
</Modal>
</div>
)
}
が、これではありません。私はsetState(…): Cannot update during an existing state transition
についての警告を受け
...
<button onClick={this.openModal()}>Open Modal</button>
...
、と誰もがそれをするだろう私にこれを説明することができる場合すばらしいです。
自動バインディングされていないため、ES6クラスでバインドする必要があります。http://blog.andrewray.me/react-es6-autobinding-and-createclass/ –