react离开页面清除数据

react离开页面时清除当前页面的数据

相关代码

routerWillLeave() {

this.setState({    myData: [],
    codeFile: [],
})

}

window.onbeforeunload = function (e) {

    e = e || window.event; 
    if (e) {
      e.returnValue = '关闭提示';      this.setState({
          myData: []
      })
      codeFileInfo = {}
    }  
    return '关闭提示';
  };

这两种方法都不行,请问应该怎么解决


呼如林
浏览 693回答 1
1回答

慕丝7291255

我利用componentWillReceiveProps这个函数,另外附加了一些业务逻辑判断,使得每次在进入页面props发生变化时,将所有sate设为空就好啦
打开App,查看更多内容
随时随地看视频慕课网APP