私はwindow.onpopstate
イベントハンドラを使用して、前後のブラウザのボタンイベントを待ち受けます。私のコードは次のようになります。window.onpopstate - このイベントハンドラを削除する必要がありますか?
componentDidMount() {
window.onpopstate = this.onBackOrForwardButtonEvent;
}
onBackOrForwardButtonEvent = (e) => {
e.preventDefault();
log.info('back or forward button pressed');
if (this.props.route.path !== '/app') {
// ... do something
}
};
私の質問です:私はこのイベントリスナーを削除する必要がありますか - おそらくcomponentWillUnmount
に?以下のような
何か:
componentWillUnmount() {
window.removeEventListener('onpopstate', this.onBackOrForwardButtonEvent, false)
}
私は場合に、コンポーネントアンマウントリスナーを削除して上記決して持っているようonPopState
を用いた例を見てきました。
例:https://github.com/ReactTraining/react-router/issues/967