2
votes

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, compared to dynamic data in state tree.

When should we persist reducer? Any example showing why it is helpful?

3
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.

3 Answers

2
votes

Perhaps a better name in the examples would be:

const persistingReducer = persistReducer(persistConfig, rootReducer)

As Al.G. mentioned in his comment, 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.

1
votes

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

0
votes

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