React Router + 后退按钮正在重置我的应用程序状态 [英] React Router + back button is resetting my app state

查看:25
本文介绍了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:

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

感谢@charlietfl 提供的信息.

Thanks @charlietfl for the info.

这篇关于React Router + 后退按钮正在重置我的应用程序状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆