javascript - 单击刷新然后返回按钮时如何登陆到现有页面
问题描述
我遇到了一个功能问题,其中我编写了一个代码,该代码阻止用户在单击后退按钮时转到上一页。对于相同的代码,当我刷新页面然后单击后退按钮时,它会返回上一页。我不希望这种体验,并且希望用户保持在同一页面中,即使用户刷新并单击后退按钮也是如此。
我编写的现有代码如下。我可以在其中加入一些东西还是需要编写一个新组件?
import React, { useEffect } from 'react'
const popupHandler = () => window.history.pushState(null, document.title, window.location.href)
const usePreventBackButton = () => {
useEffect(() => {
window.history.pushState(null, document.title, window.location.href);
window.addEventListener('popstate', popupHandler)
return () => window.removeEventListener('popstate', popupHandler)
}, [])
}
export default usePreventBackButton
解决方案
推荐阅读
- php - this.state.messages 不是函数
- pandas - Pandas resample is jumbling date order
- javascript - 一起添加日期和时间字符串变量
- bootstrap-4 - 如何使引导卡高度响应
- c# - There is an error in XML document (0, 0) - Error Deserializing XML to object
- android - Hilt using in android library
- date - 为什么“稍后”方法会导致“日期”范围内的无限范围?
- javascript - ReactJS does not loads index.html file
- oracle - ORA-01652: 无法在表空间 TEMP 中将临时段扩展 128,但有 500GB 可用
- python-3.x - 如何从小部件调用的函数更新散景下载按钮的源