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
Add a ment  | 

3 Answers 3

Reset to default 3

I 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