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