为什么使用 redux-persist 而不是手动将状态持久化到 localStorage? [英] Why use redux-persist over manually persisting state to localStorage?
问题描述
另一种提问方式是,如果您真的只想使用保存在 localStorage 中的数据启动您的应用程序(再水化)并将每个 redux 状态更改保存到 localStorage(持久化)正在使用 redux-persist 比使用你自己的解决方案更好,比如 Dan Abramov 在这里解释?
Another way to ask would be, if you really only want to start your app up with the data saved in localStorage (rehydrate) and save every redux state change to localStorage (persist) is using redux-persist any better than using your own solution like Dan Abramov explains here?
我知道 redux-persist 有很多其他功能,我自己开始使用它是为了能够使用 redux-persist-crosstab(能够处理在不同选项卡中运行的应用程序之间的更改),但是我想知道将它用于最基本的场景是否太过分了.特别是因为它更难理解并且有时会做一些有趣的事情,比如随机调用 persist/REHYDRATE
.
I understand redux-persist comes with a lot of other features, and I myself started using it to be able to use redux-persist-crosstab (to be able to deal with changes between apps running in different tabs), but I wonder if it's overkill to use it for the most basic scenario. Especially since it's harder to understand and sometimes does funny stuff, like calling persist/REHYDRATE
randomly.
我在这里遗漏了什么明显的东西吗?
Am I missing anything obvious here?
推荐答案
redux-persist
的用法取决于应用的用例.
The usage of redux-persist
depends upon the use case of the application.
首先,让我重点介绍redux-persist
使用 PersistGate 自动在
渲染组件
中提供延迟,直到状态被持久化
以及用于显示loading
组件.
Usage of PersistGate which automatically provides a delay in
rendering of the components
until the state getspersisted
along with the usage to show theloading
component.
基于多种类型持久化的自定义函数,例如 persistStore
、persistReducer
和 persistObject
Custom functions to persist based on multiple types such as persistStore
, persistReducer
and persistObject
AutoMerging
initialStates
来自基于shallow
和deep
级别的不同状态
AutoMerging
of the initialStates
from the different states based on shallow
and deep
levels
可能是blacklisting
和whitelisting
最重要的特性reducers
嵌套持久化shallow
级persistence
和deep
级persistence
坚持使用 migrations
来存储 redux-store
的不同版本.
Persisting with migrations
to store different versions of the redux-store
.
转换以支持不可变、压缩、加密、过滤等
考虑到您构建的应用程序
仅用于开发
目的,那么它最多需要PersistGate
、persistStore
> 可能还有blacklist
和whitelist
的reducer,这仍然是相当多的工作 考虑到你知道你可能会做什么需要您的应用程序.
Considering the application
you build is just for development
purposes, then it would require at most PersistGate
, persistStore
and probably blacklist
and whitelist
of the reducers, which would still be considerable amount of work considering that you are aware what you might need for your application.
现在对于 production
级别且易于扩展的应用程序,那么它至少需要我在上面列出的 5/7
功能.如果没有这个模块,你可能需要安装压缩、加密等模块,找到一种优雅地存储不同版本的redux-store
和 retrieve
和 display
什么可能符合您的要求,以及 redux-store
状态的 rehydration
.
Now for the applications that are production
level and are prone to scaling, then it would be requiring at least 5 / 7
features that I have listed above. Without this module you might need to install modules for compression, encryption and much more, find a way to gracefully store different versions of the redux-store
and retrieve
and display
what might fit your requirement, along with the rehydration
of that redux-store
state.
这还有很多含义,稍后会遇到.因此根据项目的需求,我认为使用这个包来管理redux-store storage
There are many more implications to this, which will be encountered later on. Therefore based on the requirements of the project, I think it would be beneficial to use this package, to manage the redux-store storage
这篇关于为什么使用 redux-persist 而不是手动将状态持久化到 localStorage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!