我正在使用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