react離開頁面時清除當前頁面的數據相關代碼routerWillLeave() {this.setState({ myData: [],
codeFile: [],
})}window.onbeforeunload = function (e) { e = e || window.event;
if (e) {
e.returnValue = '關閉提示'; this.setState({
myData: []
})
codeFileInfo = {}
}
return '關閉提示';
};這兩種方法都不行,請問應該怎么解決
1 回答

慕絲7291255
TA貢獻1859條經驗 獲得超6個贊
我利用componentWillReceiveProps這個函數,另外附加了一些業務邏輯判斷,使得每次在進入頁面props發生變化時,將所有sate設為空就好啦
添加回答
舉報
0/150
提交
取消