Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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?

like image 793
user3653863 Avatar asked Jan 23 '19 18:01

user3653863


People also ask

Is it good to use redux persist?

One of the best features of the redux persists is that it provides us with the PersistGate — it helps us to delay the rendering until the state has been retrieved and saved to redux. Here we can show loading components until our state gets the persisted value. 2. Another great feature it provides Blacklist & Whitelist.

What is rehydrate in redux persist?

"persist/REHYDRATE":This phase is where the persisted data stored in browser replace the data in Redux store. Across all reducers, every local state is "rehydrated" and is replaced by the persisted store. Each reducer replaces their content by the persisted one.


1 Answers

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.

like image 179
Bruno Eduardo Avatar answered Oct 20 '22 14:10

Bruno Eduardo