6
:componentWillUnmountでlistenerを正しく削除する方法に反応します。なぜコンストラクタでバインドが必要ですか?私は少し困惑している、このsintaxの違いは何ですか
constructor(props) {
super(props);
this.state = {
openPane: false
}
this.togglePaneHelper = this.togglePaneHelper.bind(this);
}
componentDidMount() {
document.body.addEventListener('click', this.togglePaneHelper);
}
componentWillUnmount() {
document.body.removeEventListener('click', this.togglePaneHelper);
}
と、この1:
constructor(props) {
super(props);
this.state = {
openPane: false
}
}
componentDidMount() {
document.body.addEventListener('click', this.togglePaneHelper.bind(this));
}
componentWillUnmount() {
document.body.removeEventListener('click', this.togglePaneHelper);
}
私の関心は、2番目の構文が正しくのリスナーを削除していないということで、
Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. Please check the code for the undefined component.
最初のアプローチは、イベントリスナーを正しく削除する方法ですか?この記事に対するコメント:https://gist.github.com/Restuta/e400a555ba24daa396cc? – claireablani
はい、最初のアプローチは、その要点で議論されているのと同じです。 –