我已经查看了我能找到的内容,但据我所知,我做事是正确的。
我的配置存储.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)
}));
我的日记还原器.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?..]
而日记.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 正在持久保存,因此当我在应用程序打开时离开应用程序时,该值仍然是 true,并且在重新启动应用程序后返回该页面时它可见。
据我所知,我正确使用了黑名单,但它对我不起作用。谁能看出我做错了什么?
我在项目中遇到了同样的问题。使用 redux-persist 的黑名单和白名单时有一个问题,因为它们的行为有点奇怪。
在代码中,您diaryPersistConfig
设置正确,但未包含persistConfig
对象。我怀疑问题出在这种配置上,这非常不直观。
您必须在组合的化简器持久配置中添加blacklist
标签,否则较低级别(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 的嵌套持久自述文件部分。