react js:检测页面刷新 [英] react js : detect page refresh

查看:142
本文介绍了react js:检测页面刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 react.js 的新手.我有一个带表格的简单页面.当我重新加载页面时,状态正在丢失.有没有办法检测浏览器刷新?

I am new to react.js. I have a simple page with table. When I reload the page, state is getting lost. Is there a way to detect the browser refresh ?

推荐答案

活动 beforeunload 在刷新窗口浏览器之前执行.活动可取消.

The event beforeunload is executed just before window browser is being refreshed. Event is cancellable.

window.beforeunload = (e) => {
  console.log('Stop this');
  e.preventDefault()
  e.returnValue = '';
};

当使用 React 时,一个选项是控制您的应用程序组件或最高阶组件,在 componentDidMount 生命周期方法中,正如@georgim 建议的那样,而不是 componentWillUnmount我首先建议并管理您想要实现的目标.

When using React an option is to take control in your Application component, or your most higher order component, in the componentDidMount lifecycle method as @georgim suggested instead componentWillUnmount as I first suggested, and manage there what you want to achieve.

这篇关于react js:检测页面刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆