Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blacklist Redux-persist in react native


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>
  )
}

like image 518
Ahmed Saeed Avatar asked Jan 20 '26 05:01

Ahmed Saeed


1 Answers

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']
}
like image 195
Ajin Kabeer Avatar answered Jan 22 '26 06:01

Ajin Kabeer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!