react离开页面时清除当前页面的数据
相关代码
routerWillLeave() {
this.setState({
myData: [],
codeFile: [],
})
}
window.onbeforeunload = function (e) {
e = e || window.event;
if (e) {
e.returnValue = '关闭提示';
this.setState({
myData: []
})
codeFileInfo = {}
}
return '关闭提示';
};
我利用componentWillReceiveProps这个函数,另外附加了一些业务逻辑判断,使得每次在进入页面props发生变化时,将所有sate设为空就好啦