Redux state reset when invoking new route using Vaadin router in Lit project.

huangapple go评论56阅读模式
英文:

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 &lt;a href=&quot;/charger&quot;&gt;Go to charger view&lt;/a&gt; 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 = &#39;/charger&#39;. 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.

huangapple
  • 本文由 发表于 2023年2月9日 02:20:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390130.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定