redux-persist黑名单被忽略

时间:2019-01-23 18:32:52

标签: react-native redux-persist

我已经仔细研究了所有内容,但据我所知我做得正确。

我的configStore.js看起来像这样:

NotesTable

我的diaryreducer.js看起来像这样:

Name

Diary.js看起来像这样:

import diaryReducer from '../reducers/diaryReducer';
[..]
const diaryPersistConfig = {
    key: 'diary',
    storage: storage,
    keyPrefix: '',
    blacklist: ['loading', 'uploadModalVisible', 'monthModalVisible', 'editModalVisible', 'entryUploading', 'deleteEntryDisabled']
};
[..]
const persistedReducer = persistReducer(persistConfig, combineReducers({
    auth: persistReducer(authPersistConfig, authReducer),
    diary: persistReducer(diaryPersistConfig, diaryReducer)
}));

uploadModalVisible将被保留,因此当我在打开应用程序的状态下离开应用程序时,该值仍为true,并且在重新启动应用程序后返回该页面时可见。

据我所知,我正确使用了黑名单,但它对我不起作用。谁能看到我做错了什么?

1 个答案:

答案 0 :(得分:2)

我在项目中遇到了同样的问题。使用redux-persist的黑名单和白名单时有一个陷阱,因为它们的行为有点怪异。

在您的代码中,您拥有diaryPersistConfig的设置权限,但没有包括persistConfig对象。我怀疑问题出在这种配置上,这是非常不直观的。

必须将blacklist标记添加到组合的reducer持久配置中,否则较低级别(diaryPersistConfig)的黑名单将被忽略。

下面的代码应该可以帮助您理解我的意思:

const diaryPersistConfig = {
    key: 'diary',
    storage: storage,
    keyPrefix: '',
    blacklist: ['loading', 'uploadModalVisible', 'monthModalVisible', 'editModalVisible', 'entryUploading', 'deleteEntryDisabled']
};

const persistConfig = {
    key: 'root',
    storage: AsyncStorage,
    blacklist: ['diary'],
};

const persistedReducer = persistReducer(persistConfig, combineReducers({
    auth: persistReducer(authPersistConfig, authReducer),
    diary: persistReducer(diaryPersistConfig, diaryReducer)
}));

有关官方示例,请查看Redux Persist的Nested Persist自述文件部分。