黑名单 Redux-在 React Native 中持久化

Blacklist Redux-persist in react native


嗨,我是 React Native 和 Redux 的新手。 我正在使用 redux persist 在本地存储数据,但有些键我不想保留。为此,我正在使用黑名单,但它不起作用。它坚持所有的钥匙,而不是忽略我想要的钥匙。这是代码

我不想坚持 datatabbar


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

你的持久配置对我来说似乎没问题。你也可以添加 whitelist 键吗?

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

您是否尝试过完全删除应用程序并在添加持久配置后进行全新安装?如果没有它,之前保存的数据仍然存在。

仔细检查后,persist 配置并不完全正确。为了让 blacklistwhitelist 工作,它们必须匹配你应用持久化配置的 reducer 的键 - 在这种情况下,rootReducer,它只有一个键 - prodReducer.

除了 root 之外,您还需要专门配置产品减速器的持久性。文档称之为 nested persists。您可以在 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),
}) 

然后您可以从主 persistConfig 中删除黑名单。

我已经解决了这个问题

现在,我已经创建了两个减速器。一种用于黑名单,一种用于白名单。 然后组合起来

export default combineReducers({
    whitelistReducer, blackListReducer
}) 

并在商店中创建一个 persistConfig,其中我在黑名单和白名单中给出了受人尊敬的 reducer

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