gpt4 book ai didi

react-native - redux-persist 黑名单被忽略

转载 作者:行者123 更新时间:2023-12-01 12:08:26 26 4
gpt4 key购买 nike

我已经浏览了我能找到的内容,但据我所知,我做的事情是正确的。

我的 configStore.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)
}));

我的 diaryreducer.js 看起来像这样:
const diaryDefaultState = {
loading: false,
uploadModalVisible: false,
monthModalVisible: false,
editModalVisible: false,
entryUploading: false,
deleteEntryDisabled: false,
entries: []
};

export default (state = diaryDefaultState, action) => {

switch (action.type) {

case 'ENTRIES_LOADING':
return {
...state,
loading: true
};
[..others, don't think these are important for storage, just use during run?..]

Diary.js 看起来像这样:
//in render()
<Modal
animationType="slide"
onRequestClose={this.onCloseModal}
transparent={false}
visible={this.props.uploadModalVisible}
>
[....]
const mapStateToProps = (state) => {

return {
user: state.auth.user,
loading: state.diary.loading,
uploadModalVisible: state.diary.uploadModalVisible,
monthModalVisible: state.diary.monthModalVisible,
editModalVisible: state.diary.editModalVisible,
entryUploading: state.diary.entryUploading,
deleteEntryDisabled: state.diary.deleteEntryDisabled,
entries: state.diary.entries
};

};

uploadModalVisible 被持久化,所以当我在应用程序打开时离开它时,该值仍然为真,并且当我重新启动应用程序后返回该页面时它是可见的。

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

最佳答案

我在我的项目中遇到了同样的问题。使用 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自述文件部分。

关于react-native - redux-persist 黑名单被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54333565/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com