简体   繁体   English

如何缓存 Redux 状态? (没有 redux-persist)

[英]How can I cache Redux states? (without redux-persist)

Problem问题

I want to save data in the store with Redux, like tokens and user-related data to be used across the whole application (I'm using React).我想使用 Redux 将数据保存在存储中,例如要在整个应用程序中使用的令牌和用户相关数据(我正在使用 React)。

The problem is that Redux does not persist in the state when refreshing a page.问题是 Redux 在刷新页面时不会保持状态。

Thoughts想法

  • I'm not convinced about using redux-persist since the package doesn't seem very active.我不相信使用 redux-persist 因为这个包看起来不是很活跃。
  • I've seen RTK Query, but from what I'm understanding, I still have to make the API call to get the data (even if the actual call is not made and gets the cached data).我见过 RTK Query,但据我了解,我仍然需要进行 API 调用才能获取数据(即使没有进行实际调用并获取缓存的数据)。 Seems too much of an overkill.似乎太过分了。

Maybe I'm missing some core Redux or React concept?也许我错过了一些核心 Redux 或 React 概念? It feels a bit crazy for me not to be able to find a go-to package (or any other method) just to save a piece of data and be able to access it across the whole application without resetting on refresh :/无法找到一个首选包(或任何其他方法)只是为了保存一段数据并能够在整个应用程序中访问它而无需在刷新时重置:/

Redux-persist doesn't need to be very active - it works for half a decade now and there is just no need for a lot of changes. Redux-persist 不需要非常活跃——它现在可以工作五年,而且不需要进行很多更改。 It works.有用。

It is the to-go-package for that purpose.它是为此目的而准备的包。

尝试将您的 Redux 状态保存在 localStorage

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

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