REACT路由 - React-Router-Dom PushState()呼叫历史记录发生了很多次

发布于 2025-01-31 06:42:59 字数 1604 浏览 1 评论 0原文

我正在使用React-Router-Dom(V6),并注意到我无法按预期使用“浏览器”按钮。似乎我的应用程序(使用React/Redux)将位置多次推到浏览器历史记录时,当我将其路由到应用程序中的其他位置时。

这也导致Safari的申请崩溃:

SecurityError:尝试使用history.pushstate()每30秒超过100次

代码看起来像这样:

app.tsx

...
<BrowserRouter>
   <Routes>
      <Route path={ROUTES.auth.login} element={<PublicRoute component={Login} />} />
      <Route path={ROUTES.auth.register} element={<PublicRoute component={Register} />} />
      <Route path={ROUTES.dashboard} element={<PrivateRoute component={Dashboard} />} />
   </Routes>
</BrowserRouter>
...

publicRoute.tsx

...
export default function PublicRoute({ propsForComponent = {}, ...props }: PublicRouteProps): ReactElement {
    const { showLoadingScreen } = useSelector((state: RootState) => ({
        showLoadingScreen: state.loading.showLoadingScreen,
    }));

    const { component: Component } = props;

    if (showLoadingScreen) return <LoadingOverlay />;

    return <Component {...propsForComponent} />;
}
...

我发现< EM> PublicRoute 组件由于使用UseElector钩与Redux Store的连接而被多次调用。

showloadingscreen 在应用程序试图获取用户时将更新此变量。

因此,我的问题现在是:如何防止多个重新租户,这会导致 history.pushstate()调用?

我知道我可以将 loadingoverlay 组件移至其他入口点。但是在私有中,我也已连接到redux状态,因为我需要访问用户对象。 使用 PublicRoute 组件显示了最小的例子...)

(我 干杯!

I am using react-router-dom (v6) and noticed that I cannot use the browsers back button as expected. It seems like my application (using react/redux) pushes the location multiple times to the browser history when I am routing to a different location in my app.

This results in an application crash in Safari too:

SecurityError: Attempt to use history.pushState() more than 100 times per 30 seconds

The code looks something like this:

App.tsx

...
<BrowserRouter>
   <Routes>
      <Route path={ROUTES.auth.login} element={<PublicRoute component={Login} />} />
      <Route path={ROUTES.auth.register} element={<PublicRoute component={Register} />} />
      <Route path={ROUTES.dashboard} element={<PrivateRoute component={Dashboard} />} />
   </Routes>
</BrowserRouter>
...

PublicRoute.tsx

...
export default function PublicRoute({ propsForComponent = {}, ...props }: PublicRouteProps): ReactElement {
    const { showLoadingScreen } = useSelector((state: RootState) => ({
        showLoadingScreen: state.loading.showLoadingScreen,
    }));

    const { component: Component } = props;

    if (showLoadingScreen) return <LoadingOverlay />;

    return <Component {...propsForComponent} />;
}
...

I found out that the PublicRoute component gets called multiple times due to the connection to the redux store with the useSelector hook.

showLoadingScreen gets updated when the application is trying to fetch the user, during this time this variable changes.

So my question is now: how do I prevent multiple re-renders, which causes the history.pushState() calls?

I know I could move the LoadingOverlay component to a different entry point. But In PrivateRoute I am also connected to the redux state because I need to access the user object. (I used the PublicRoute component to show a minimal example of whats going on...)

Any help would be highly appreciated,
cheers!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文