gpt4 book ai didi

javascript - 不要在新页面加载或刷新时渲染 React 组件

转载 作者:行者123 更新时间:2023-11-30 16:27:23 25 4
gpt4 key购买 nike

如果页面刚刚加载或刷新,我正在寻找一种方法让 React 不渲染组件。仅当用户在单页应用程序中逐页浏览时,组件才应呈现。

我已经尝试使用 window.document.readyState 来检测这一点,但无论成功与否。

最佳答案

你可以像这样在浏览器中使用 session 存储

if(!sessionStorage.active || sessionStorage.active == window.location.href) {
// First visit or refresh
} else {
// Render component
}
sessionStorage.active = window.location.href;

当用户第一次访问页面时,sessionStorage.active不会被设置,它将转到 // First visit or refresh .

如果用户刷新页面,sessionStorage.active将被设置,但它也将等于当前 url 并转到 // First visit or refresh .

如果用户从站点内的另一个页面访问该页面,sessionStorage.active将被设置,它不会匹配 window.location.href因为它当前设置为以前访问过的页面 url。因此它将转到 // Render component .

此解决方案可能存在一些必须针对特殊用例解决的怪癖,但希望您有一个好的起点。

关于javascript - 不要在新页面加载或刷新时渲染 React 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33902419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com