MyN*_*ame 6 javascript reactjs react-router
当用户刷新其他页面时,我需要重定向到主页。这是我尝试为页面刷新添加事件侦听器。但它不起作用,我Router.js?cf42:125 Uncaught RangeError: Maximum call stack size exceeded在浏览器中。你能告诉我如何处理这个错误吗?
import {browserHistory} from 'react-router';
class MyComponent extends React.Component {
constructor(props, context) {
super(props, context);
this.onUnload = this.onUnload.bind(this);
}
componentDidMount() {
window.addEventListener('beforeunload', this.onUnload);
}
componentWillUnmount() {
window.removeEventListener('beforeunload', this.onUnload);
}
onUnload() {
browserHistory.push('/');
}
render() {
...
}
}
Run Code Online (Sandbox Code Playgroud)
UPD:我已经修复了 RangeError。现在MyComponent的页面被重定向到主页,但之后它再次打开。我认为这是因为重定向不会停止页面刷新。
您需要绑定onUnload()- 将以下内容添加到您的构造函数中:
this.onUnload = this.onUnload.bind(this);
Run Code Online (Sandbox Code Playgroud)
此外,侦听器的正确事件名称是onbeforeunload
| 归档时间: |
|
| 查看次数: |
11572 次 |
| 最近记录: |