为什么使用 redux-persist 而不是手动将状态持久化到 localStorage? [英] Why use redux-persist over manually persisting state to localStorage?

查看:14
本文介绍了为什么使用 redux-persist 而不是手动将状态持久化到 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 gets persisted along with the usage to show the loading component.

基于多种类型持久化的自定义函数,例如 persistStorepersistReducerpersistObject

Custom functions to persist based on multiple types such as persistStore, persistReducer and persistObject

AutoMerging initialStates 来自基于shallowdeep 级别的不同状态

AutoMerging of the initialStates from the different states based on shallow and deep levels

可能是blacklistingwhitelisting最重要的特性reducers

嵌套持久化shallowpersistencedeeppersistence

坚持使用 migrations 来存储 redux-store 的不同版本.

Persisting with migrations to store different versions of the redux-store.

转换以支持不可变压缩加密过滤

考虑到您构建的应用程序仅用于开发目的,那么它最多需要PersistGatepersistStore> 可能还有blacklistwhitelist 的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 和 retrievedisplay 什么可能符合您的要求,以及 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆