Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redux-persist - how do you blacklist/whitelist nested state

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

like image 257
David Browning Avatar asked Aug 02 '18 23:08

David Browning


Video Answer


3 Answers

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

like image 87
Wayne Bloss Avatar answered Sep 20 '22 15:09

Wayne Bloss


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?

like image 45
Stich Avatar answered Sep 20 '22 15:09

Stich


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

like image 28
Piotr Kujawa Avatar answered Sep 20 '22 15:09

Piotr Kujawa