Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React native + redux-persist: how to ignore keys (blacklist)?

I'm storing my settings with redux-persist and would like to ignore some of them to have them reset on every restart, e.g. after a crashing.

It's possible to add an array of reducer-names as blacklist or whitelist, but I'd like to ignore specific keys, e.g. settings.isLoggedIn instead of settings.

// ...
function configureStore(initialState) {
    const store = createStore(
        RootReducer,
        initialState,
        enhancer
    );

    persistStore(store, {
        storage: AsyncStorage,
        blacklist: ['router', 'settings'] // works, 'settings.isLoggedIn' doesn't.
    }, () => {
        // restored
    });

    return store;
}
// ...

Do I have to create another reducer or does anyone a solution to this problem?

Thanks in advance!

like image 589
Mr. B. Avatar asked Dec 17 '16 10:12

Mr. B.


People also ask

Can I use redux persist in react native?

Redux Persist and React NativeIn React Native applications, data can be persisted locally using AsyncStorage . AsyncStorage is an asynchronous, persistent, key-value storage system that is global to the entire app. Redux Persist is a tool used to seamlessly save the application's Redux state object to AsyncStorage .

Is it good to use redux persist?

One of the best features of the redux persists is that it provides us with the PersistGate — it helps us to delay the rendering until the state has been retrieved and saved to redux. Here we can show loading components until our state gets the persisted value. 2. Another great feature it provides Blacklist & Whitelist.


3 Answers

As per the documentation, the blacklist parameter contains: 'keys (read: reducers) to ignore', so I am afraid it is not possible to implement the behaviour that you want. You can try and implement that functionality yourself, but I think the codebase of the package is really focused on blacklisting reducers instead of properties (see this). I am afraid that the only solution is to create a separate reducer for your non-persistent keys (in my experience it is not much of a hassle).

like image 165
martinarroyo Avatar answered Oct 17 '22 20:10

martinarroyo


Use transforms for save separate fields, for example for username in redux-form MyForm inside state.form.MyForm:

const formName = `MyForm`

const formTransform = createTransform(
  (inboundState, key) => {
    return {
      ...inboundState,
      [formName]: {
        values: {
          username: _.get(inboundState, `${ MyForm }.values.username`)
        }
      }
    }
  },
  (outboundState, key) => {
    return outboundState
  },
  { whitelist: [`form`] }
)

persistStore(store, {
  whitelist: [
    `form`
  ],
  transforms: [
    formTransform
  ]
})
like image 6
lokhmakov Avatar answered Oct 17 '22 20:10

lokhmakov


You can use Nested Persists for this.

import { persistStore, persistReducer } from 'redux-persist';


const rootPersistConfig = {
  key: 'root',
  storage: storage,
  blacklist: ['auth']
}

// here you can tell redux persist to ignore loginFormData from auth reducer

const authPersistConfig = {
  key: 'auth',
  storage: storage,
  blacklist: ['loginFormData']
}

// this is your global config
const rootReducer = combineReducers({
  auth: persistReducer(authPersistConfig, authReducer),
  other: otherReducer,
})

// note: for this to work, your authReducer must be inside blacklist of 
// rootPersistConfig

const myReducerConfig = {
  key: "cp",
  storage: storage,
  blacklist: ["authReducer"],
  debug: true
};


like image 5
shubham choudhary Avatar answered Oct 17 '22 19:10

shubham choudhary