redux-persist黑名单被忽略 [英] redux-persist blacklist being ignored
问题描述
我已经仔细研究了可以找到的内容,但据我所知我做得正确.
I've looked through what I could find on this but as far as I can tell I am doing things correctly.
我的configStore.js看起来像这样:
My configStore.js looks like this:
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看起来像这样:
My diaryreducer.js looks like this:
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看起来像这样:
And Diary.js looks like this:
//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,并且在重新启动应用程序后返回该页面时可见.
uploadModalVisible is being persisted so when I leave the app while it is open, the value is still true and it is visible when I return to that page after re-launching the app.
据我所知,我正确使用了黑名单,但它对我不起作用.谁能看到我做错了什么?
As far as I can tell I'm using the blacklist correctly but it's not working for me. Can anyone see what I've done wrong?
推荐答案
我在项目中遇到了同样的问题.使用redux-persist的黑名单和白名单时有一个陷阱,因为它们的行为有点怪异.
I faced the same problem in my project. There's a catch when using redux-persist's blacklist and whitelist, because their behavior is a bit weird.
在您的代码中,您具有diaryPersistConfig
设置权限,但没有包括persistConfig
对象.我怀疑问题出在这种配置上,这是非常不直观的.
In your code you have diaryPersistConfig
setup right, but you didn't include your persistConfig
object. I suspect the problem is in that configuration, which is super unintuitive.
必须将blacklist
标签添加到组合的reducer持久配置中,否则较低级别(diaryPersistConfig
)黑名单将被忽略.
You must add a blacklist
tag to the combined reducer persist configuration, otherwise the lower level (diaryPersistConfig
) blacklist will be ignored.
下面的代码应该可以帮助您理解我的意思:
The code below should help you understand what I mean:
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的嵌套的波斯人 README部分.
For an official example check out Redux Persist's Nested Persist README section.
这篇关于redux-persist黑名单被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!