0
votes

Hi i am new to react native and redux. I am using redux persist to store data locally, but there are some keys that i don't want to persist. For that i am using blacklist, which is not working. It persist all the keys and not ignoring the keys that i want. Here is the code

I dont want to persist data and tabbar.


store.js

import rootReducer from './reducers'
// import AsyncStorage from '@react-native-community/async-storage';
import { AsyncStorage } from 'react-native';
import { persistStore, persistReducer } from 'redux-persist'

const persistConfig = {
    key: 'root',
    storage: AsyncStorage,
    blacklist: ['data', 'tabbar'],
}

const persistedReducer = persistReducer(persistConfig, rootReducer)

let store = createStore(persistedReducer)

let persistor = persistStore(store)

export { store, persistor }.

reducers/product.js


const initialState = {
    products: [],
    favs: [],
    data: [],
    tabbar: false
}

const products = (state = initialState, action) => {
    switch (action.type) {
        case SET_PRODUCTS:
            return {
                ...state,
                products: action.products
            }
        case ADD_TO_FAV:
            return {
                ...state,
                favs: [...state.favs, action.product]
            }
        case REMOVE_FROM_FAV:
            return {
                ...state,
                favs: state.favs.slice().filter(f => f._id != action.product._id)
            }
        case SET_DATA:
            return {
                ...state,
                data: [...state.data, action.data]
            }
        case TABBAR:
            return {
                ...state,
                tabbar: action.tabbar
            }
        default:
            return state;
    }
}

export default products;

reducers/index.js

import prodReducer from './products';

export default combineReducers({
    prodReducer
})  

Action/product.js

export const SET_PRODUCTS = 'SET_PRODUCTS';
export const ADD_TO_FAV = 'ADD_TO_FAV';
export const REMOVE_FROM_FAV = 'REMOVE_FROM_FAV';
export const SET_DATA = 'SET_DATA';
export const TABBAR = 'TABBAR';



export const setProducts = (products) => {
    return {
        type: SET_PRODUCTS,
        products
    };
}

export const addToFav = (product) => {
    return {
        type: ADD_TO_FAV,
        product
    };
}
export const removeFromFav = (product) => {
    return {
        type: REMOVE_FROM_FAV,
        product
    };
}

export const tabbar = (tabbar) => {
    return {
        type: TABBAR,
        tabbar
    };
}

export const setData = (data) => {
    return {
        type: SET_DATA,
        data
    };
}

app.js

import React, { useEffect } from 'react';
import Navigator from './navigation/Navigator'
import { Provider } from 'react-redux';
import { store, persistor } from './redux/store';
import { PersistGate } from 'redux-persist/integration/react'
import { firebase } from '@react-native-firebase/messaging'
import AsyncStorage from '@react-native-community/async-storage';


  return (
    <Provider store={store}>
      <PersistGate loading={null} persistor={persistor}>
        <StatusBar backgroundColor={'#AD1457'} />
        <Navigator />
      </PersistGate>
    </Provider>
  )
}

4

4 Answers

0
votes

Your persist config seems alright to me. Can you add the whitelist key too?

const persistConfig = {
    key: 'root',
    storage: AsyncStorage,
    blacklist: ['data', 'tabbar'],
    whitelist:['products','favs']
}
0
votes

Have you tried removing the app completely and doing a fresh install after the persist config has been added? Without that, data persisted previously is still there.

0
votes

On closer inspection, the persist config is not entirely correct. In order for blacklist and whitelist to work, they have to match keys of a reducer to which you apply the persist config - in this case, rootReducer, which only has one key - prodReducer.

What you want is configure persistence of your products reducer specifically in addition to root. The docs call this nested persists. You can do this in your reducers/index.js:

import AsyncStorage from '@react-native-community/async-storage';
import { persistStore, persistReducer } from 'redux-persist'

import prodReducer from './products';

const productsPersistConfig = {
  key: 'products',
  storage: AsyncStorage,
  blacklist: ['data', 'tabbar'],
}

export default combineReducers({
    prodReducer: persistReducer(productsPersistConfig, prodReducer),
}) 

You can then remove the blacklist from your main persistConfig.

0
votes

I have fixed this issue

Now, i have created two reducers . One for blacklist and one for whitelish. Then combine it

export default combineReducers({
    whitelistReducer, blackListReducer
}) 

and in store create a persistConfig in which i gave the respected reducer in blacklist and whitelist

const persistConfig = {
    key: 'root',
    storage: AsyncStorage,
    whitelist: ['whitelistReducer'],
    blacklist: ['blackListReducer']
}