英文:
Redux state reset when invocing new route using Vaadin router in Lit project
问题
我正在使用Redux进行状态管理和Vaadin路由器进行单页应用路由。如果我通过点击<a href="/charger">转到充电器视图</a>
来调用一个新的路由,充电器视图会在路由器出口中渲染,状态保持不变。问题出现在当我想要通过更改地址字段或像这样更改document.location.href = '/charger';
来调用一个新的路由时。在这种情况下,Redux存储会被清除。有人知道为什么以及如何解决吗?
英文:
I am using redux for state management and Vaadin router for SPA routing. If I invoke av new route by clicking a <a href="/charger">Go to charger view</a>
the charger view renders in the router outlet and state remains intact. The problem arise when I want to invoke a new route changing the adress field or changing as document.location.href = '/charger'
. In this case the redux store gets cleared. Does anyone know why and how to solv it?
答案1
得分: 1
手动更改地址字段或设置document.location.href
会导致浏览器从头开始渲染页面。要在这些操作之间保持状态的唯一方法是将状态持久化存储在redux存储中,例如本地存储或会话存储中。
有像redux-persist
这样的库可以帮助你做到这一点,或者你可以自己编写代码实现。
英文:
Manually changing the address field or setting document.location.href
causes the browser to render the page from scratch. The only way to keep the state across those would be to persist the states in the redux store in a persistent storage like local storage or session storage.
There are libraries like redux-persist
that help you do this or you could write your own.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论