简体   繁体   English

当我在地址栏中更改路由时,React-Router正在刷新页面

[英]React-Router is refreshing page when I change the route in the address bar

I am using redux and react-redux to change the state of my application. 我正在使用redux和react-redux来更改应用程序的状态。

I have two components, Login and Home . 我有两个组件, LoginHome

I will dispatch an action to update the state if the user is correctly logged and navigate him to the Home component. 如果用户正确登录,我将调度一个操作来更新状态,并将其导航到Home组件。 I am using an authguard in the route of Home component to check whether the user is logged or not. 我在Home组件的路由中使用authguard来检查用户是否已登录。

But when I navigate to the Home component by typing the route in the address bar, the page is refreshing and the state changes to its initial values. 但是,当我通过在地址栏中键入路由导航到Home组件时,页面将刷新,并且状态更改为其初始值。

How can I maintain the state values even if I navigate to a component by using the address bar? 即使使用地址栏导航到组件,如何维护状态值?

You'll have to store the data in localstorage if you are navigating to a component by using the address bar. 如果要使用地址栏导航到组件,则必须将数据存储在localstorage When you are navigating by address bar it is kind of like refreshing the page and the entire store is lost. 当您通过地址栏导航时,有点像刷新页面,整个商店都丢失了。 You'll have to hydrate the store again for it to show the previous redux state. 您必须再次为商店充水,以显示先前的还原状态。

You can use middlewares such as redux-persist or redux-storage which will save the redux store and hydrate the redux store on page reload. 您可以使用中间件,例如redux-persistredux-storage ,它们将保存redux存储并在页面重新加载时使redux存储水化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM