更改url时带有路由器的redux保持状态 [英] redux with router keep state when changing url

查看:38
本文介绍了更改url时带有路由器的redux保持状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个问题:

我在 redux 中封装了一个简单的路由技术,如下所示:

I have a simple routing technique wrapped inside redux, like this:

<Provider store={store}>
    <Router>
      <React.Fragment>
        <Route exact path="/" component={App} />
        <Route path="/about" component={About} />
      </React.Fragment>
    </Router>
  </Provider>

我的App组件中有一个简单的Link,就像这样

And I have a simple Link in my App component like this

<Link to="/about">About</Link>

当我通过单击 Link 导航到 About 时,一切正常,我的商店没有重置,但问题是:当我在地址栏中手动输入 URL 或通过刷新页面(按 F5)时,有什么方法可以保持状态?

when I navigate to About by clicking the Link, everything is fine and my store is not reset, but here's the question: Is there any way to keep the state when I enter the URL in address bar manually or by refreshing the page (hitting F5)?

真的谢谢!

推荐答案

Redux 商店应用程序将在您刷新页面后立即松散状态:

Redux store application will loose state as soon as you refresh the page:

如果你想在页面刷新后存储你想要的东西,请使用所有浏览器中存在的本地存储.

If you want to store something which you want even after page refresh use local storage present in all browsers.

redux 中有一种叫做热重载的东西,它不会在刷新后丢失数据.

There is something called hot reloading in redux which will not loose data post refresh.

但由于您当前的代码存储将重置,这是预期行为.

But with your current code store will reset, this is expected behavior.

这篇关于更改url时带有路由器的redux保持状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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