redux-persist blacklist being ignored

2019-07-23 15:10发布

问题:

I've looked through what I could find on this but as far as I can tell I am doing things correctly.

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

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?..]

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 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?

回答1:

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.

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.

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

For an official example check out Redux Persist's Nested Persist README section.