admin管理员组文章数量:1287894
The redux-persist library provides ways to store redux state tree into some sorts of storage, and rehydrate when app is re-opened.
I see the need of restoring the state tree because it contains useful data, but the library also includes a feature for persisting reducers.
const persistedReducer = persistReducer(persistConfig, rootReducer)
I cannot quite understand the motivation behind since I believe reducers are just functions to mutate the state. Those are well-defined in the code, pared to dynamic data in state tree.
When should we persist reducer? Any example showing why it is helpful?
The redux-persist library provides ways to store redux state tree into some sorts of storage, and rehydrate when app is re-opened.
I see the need of restoring the state tree because it contains useful data, but the library also includes a feature for persisting reducers.
const persistedReducer = persistReducer(persistConfig, rootReducer)
I cannot quite understand the motivation behind since I believe reducers are just functions to mutate the state. Those are well-defined in the code, pared to dynamic data in state tree.
When should we persist reducer? Any example showing why it is helpful?
Share Improve this question asked Aug 3, 2018 at 18:29 NakamuraNakamura 1,0312 gold badges13 silver badges23 bronze badges 1-
According to the docs,
persistReducer
"returns an enhanced reducer". I guess this means that it wraps your reducer into a function that will persist the new state for you automatically. – Al.G. Commented Aug 3, 2018 at 18:34
3 Answers
Reset to default 3I have used redux-persist before and it has a use-case where you want to use it.
Let's say you are creating a facebook clone website. Then you go to your profile and fetch all your post. Now, if the user refresh the website, it will refresh your redux to the initial state right? you might need to reload every data again which is not optimal in this case.
With Redux-persist, when you refresh your web, it can bring up the latest redux session and you do not need to fetch anything again. Therefore, when user refresh, it already loaded with the post and profile again.
Let me know if you still confuse about it
Perhaps a better name in the examples would be:
const persistingReducer = persistReducer(persistConfig, rootReducer)
As Al.G. mentioned in his ment, persistReducer returns an enhanced reducer that wraps the rootReducer you pass in and will persist that reducer's state according to the config you pass in.
The reducers themselves are not persisted since they are just functions.
You can use persisted data to load information so fast as dynamic menus, you can pre load menus from your persisted data while app is doing request to get info, you can save sessions if token is expired you just need refresh your token and refresh persisted data
本文标签: javascriptreduxpersistWhen to persist reducerStack Overflow
版权声明:本文标题:javascript - redux-persist - When to persist reducer? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741332481a2372847.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论