redux-persist - how do you blacklist/whitelist nested state
Asked Answered
P

6

13

So I have a credentials object which contains a password and a username

payload: Object
  credentials: Object
    password: ""
    username: ""

and I want to blacklist password in the reducer configuration, something like

const authPersistConfig = {
    key: 'AuthReducer',
    storage: storage,
    blacklist: ['credentials.password']
};

If I use this code, both the credential states end up being blacklisted. I want to persist the username but not the password.

It might be that redux-persist only persists top-level state or it might be a syntax error, or something else entirely - any ideas?

Many thanks

Preposition answered 2/8, 2018 at 23:48 Comment(0)
M
2

You can use the NPM package called redux-persist-transform-filter as described in redux-persist issue #277 - Whitelist reducer subproperty only?

Mangan answered 31/8, 2018 at 18:0 Comment(0)
E
1

Or if you want not completely remove the keys but just clear any of data in any nested state objects, here is example using createTransform from 'redux-persist'

How to make redux-persist blacklist for nested state?

Exospore answered 11/9, 2020 at 10:40 Comment(0)
T
1

You can do nested persit config.

const rootPersistConfig: any = {
  key: "root",
  timeout: 500,
  storage: AsyncStorage,
  blacklist: ["credentials"],
};
const credentialsPersistConfig: any = {
  key: "credentials",
  storage: AsyncStorage,
  whitelist: ["username"],
};
const rootReducer = combineReducers({
  credentials: persistReducer(credentialsPersistConfig, CredentialReducer),
});
const persistedReducer = persistReducer(rootPersistConfig, rootReducer);
Trio answered 16/3, 2021 at 16:45 Comment(2)
Password should be blacklisted in this case, right?Disfeature
I misunderstood it. but now I have edited the answer.Trio
C
1

Instead of persist root key like this :

    const rootPersistConfig = {
  key: 'root',
  storage,
}

We can use different reducer key:

 const userPersistConfig = {
  key: 'userReducer',
  storage,
  whitelist: ['user', 'isLoading']
}
const cartPersistConfig = {
  key: 'cart',
  storage
}
const rootReducer = combineReducers({
  cart: persistReducer(cartPersistConfig, cart),
  filter,
  userReducer: persistReducer(userPersistConfig, userReducer),
  wishlist,
  
})

store.js file:

 const store = configureStore({
    reducer: rootReducer,
    middleware: (getDefaultMiddleware) => {
        return getDefaultMiddleware({
            serializableCheck: false,
        }).concat(apiSlice.middleware);
    },
});
const persistor = persistStore(store);
Counterblast answered 4/9, 2023 at 10:55 Comment(0)
D
0

You can use either pick or omit to whitelist or blacklist any nested value of a particular one-level store entry. Here's an example:

let blacklistTransform = createTransform((inboundState, key) => {
    if (key === 'credentials') {
        return omit(inboundState, ['password']);
    } else {
        return inboundState;
    }
});

const persistConfig = {
    key: 'root',
    storage: AsyncStorage,
    blacklist: ['credentials'], // Avoid credentials entry
    transforms: [blacklistTransform],
};

Whitelist example:

// Just persist dropdown default values
const whitelistTransform = createTransform((inboundState, key) => {
    // Select values from the route reducer
    if (key === 'route') {
        return pick(inboundState, [
            'lastSelectedSchoolYear , lastSelectedSite',
            'lastSelectedState',
        ]);
    }
    return inboundState;
});

const persistConfig = {
    key: 'root',
    storage,
    transforms: [whitelistTransform],
};
Disfeature answered 6/8, 2021 at 19:0 Comment(2)
Where is the omit coming from?Impeller
Lodash: docs-lodash.com/v4/omitDisfeature
C
0

I recently released an open-source package called Redux Deep Persist, which can be very helpful in creating Redux Persist config for nested state no matter how deep it is. It can help you in this specific situation.

This notation blacklist: ['credentials.password'] is only allowed when you use getPersistConfig method.

Here is what your config would look like:

    import { getPersistConfig } from 'redux-deep-persist';
    import storage from "redux-persist/es/storage/session";

    const config = getPersistConfig({
        key: 'root',
        storage,
        blacklist: [
            'credentials.password',
        ],
        rootReducer, // your root reducer must be also passed here
        ... // any other props from the original redux-persist config omitting the stateReconciler
    })

You can read more in the official documentation of redux-deep-persist https://github.com/PiotrKujawa/redux-deep-persist/blob/master/README.md

Calk answered 3/4, 2022 at 11:58 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.