监听浏览器刷新操作

场景:

用户点击浏览器刷新按钮时,清除state缓存

思路:

通过 监听 beforeunload 事件

方案:

React.useEffect(() => {
  // 初始化
  const handleInit = () => {
    history.replace({...history.location, state: { ...history.location.state, propertyId: null, propertyName: null, propertyCode: null }});
    ds.queryDataSet.reset();
  };

  window.addEventListener('beforeunload', handleInit);
  return () => {
    window.removeEventListener('beforeunload', handleInit);
  };
}, []);

.

posted @ 2022-08-11 23:57  每天都要进步一点点  阅读(338)  评论(0)    收藏  举报