Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to whitelist nested state field? #1229

Open
sos1ck opened this issue Jul 21, 2020 · 2 comments
Open

How to whitelist nested state field? #1229

sos1ck opened this issue Jul 21, 2020 · 2 comments

Comments

@sos1ck
Copy link

sos1ck commented Jul 21, 2020

I want to save in storage one full reducer, and only one field from other reducer. How can I do this? I found e.g. this gh issue, but all attempts (from gh link and other sources) turned out to be a failure.

const persistConfig = { key: "root", storage, whitelist: ["first"], transforms: [createWhitelistFilter("second", ["counter"])] };

codesandbox example

It works for first reducer, nothing happens with the second, when I want to storage only counter field from reducer.

Any Ideas?

@peterdu98
Copy link

peterdu98 commented Jan 15, 2021

For those who are looking for the solution, you can have a look at this website. This solution works well in my case (react-redux v6).

Basically, there are three additions in your code, especially the storeConfig.js file.

  1. Add reducer(s) that you want to store some fields of it in the blacklist of rootPersistConfig. (e.g. loginReducer)
  2. Create a variable storing the persistConfig for that reducer. (e.g. loginPersistConfig)
  3. Add persistReducer(loginPersistConfig, loginReducer) into rootReducer

@PiotrKujawa
Copy link

PiotrKujawa commented Mar 25, 2022

Here is what you can do using my package redux-deep-persist

import { getPersistConfig } from 'redux-deep-persist';

const config = getPersistConfig({
    key: 'root',
    storage: LocalStorage, // whatever storage you use
    whitelist: [
        'a1.b1.c1',  
        'a2.b2.c2',  
        'a3',
        'a4.b4.c4.no.matter.how.deep.you.go
    ],
    rootReducer, // your root reducer must be also passed here
    ... // any other props from original redux-persist config, omitting the stateReconciler
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants