Search code examples
reduxnestedpersist

How can I persist nested redux store


I want to persist nested object of my redux store. I tried https://github.com/rt2zz/redux-persist package but it doesn't work in my case. I wonder if it's possible to define a whitelist like this: 'user.statuses.verification.isDone'

This is my store:

{
    user: {
        statuses: {
            verification: { isPending: true, isDone: false },
            activation: { isPending: true, isDone: false },
            set1: { isPending: true, isDone: false, refNumber: xxx },
            set2: { isPending: true, isDone: false, refNumber: xxx },
        },
    },
}

I want to persist only "isDone" in every of statuses and "refNumber". Can anyone help me?

I already tried nested persist as described in redux persist documentation https://github.com/rt2zz/redux-persist#nested-persists but looks like it has a limitation to 2 levels.


Solution

  • I tried this https://stackoverflow.com/a/71616665 and it works perfectly. 
In this example you can see the blacklist but you just need to replace it with the whitelist.

    const config = getPersistConfig({
        key: 'root',
        storage: AsyncStorage,
        whitelist: [
            'user.statuses.verification.isDone’,  
            'user.statuses.activation.isDone’,  
            'user.statuses.set1.isDone’,
            'user.statuses.set1.refNumber’,
            'user.statuses.set2.isDone’,
            'user.statuses.set2.refNumber’,
        ],
        rootReducer, // your root reducer must be also passed here
        ... // any other props from the original redux-persist config omitting the stateReconciler
    })