Search code examples
javascriptreactjsreact-nativereduxredux-persist

redux-persist - When to persist reducer?


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?


Solution

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