React Router + 后退按钮正在重置我的应用程序状态

编程入门 行业动态 更新时间:2024-10-25 12:23:20
本文介绍了React Router + 后退按钮正在重置我的应用程序状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用 React Router Dom 和 BrowserRouter 对象为我正在构建的站点启用基于浏览器的导航.导航运行良好,但如果用户点击后退按钮,我应用的整个状态都会被清除.

I'm using React Router Dom and the BrowserRouter object to enable browser based navigation for a site I'm building. Navigation works well, but if a user hits the back button, the entirety of my App's state is wiped clean.

我不确定为什么会发生这种情况.我试过四处寻找,但我找到的所有答案都没有提到应用程序状态重置的原因.

I'm not sure why this is happening. I've tried searching around, but all of the answers I've found don't mention why App state is getting reset.

这是我的实现:

<BrowserRouter> <Switch> <Route path="/" exact render={()=><LandingPage/>} <Route path="/about" render={()=><About/>} </Switch> </BrowserRouter>

我做错了什么吗?任何有使用 React Router 经验的人都可以给我一些建议吗?非常感谢!

Am I doing something glaringly wrong? Could anyone with experience working with React Router give me some advice? Thanks so much!

推荐答案

如果像我这样的人将来遇到这个问题并且从 Google 来到这里,这是发生了什么:

If someone like me has this problem in the future and comes here from Google, here's what's going on:

  • React 路由器不会在页面加载之间存储状态.它只是在组件之间路由流量并处理历史记录等.
  • 您需要一个单独的状态管理存储来保存状态更改.(推荐使用 Redux).
  • 感谢@charlietfl 提供的信息.

    Thanks @charlietfl for the info.

    更多推荐

    React Router + 后退按钮正在重置我的应用程序状态

    本文发布于:2023-11-24 18:55:30,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1626427.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:应用程序   按钮   状态   React   Router

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!