React Redux Persist is rehydrating, but no stored data is retrieved, instead, default

若如初见. 提交于 2020-12-15 08:53:16

问题


I've been struggling with this problem for two days, so I need help!

I have a React web in wich i've added Redux Persist (also Redux Saga for handling requests) as the documentation said.

I'm testing with a store that doesn't have any saga in the middle, when I trigger an action, the data is updated, I can see it in the Debugger, but when i refresh, despite the Redux Hydrate process runs, that value goes back to the default one.

store.js

import { createStore, applyMiddleware, compose } from 'redux';
import createSagaMiddleware from 'redux-saga'
import { persistStore, persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage';
import autoMergeLevel2 from 'redux-persist/lib/stateReconciler/autoMergeLevel2';

import rootReducer from './reducers';  
// import rootSaga from './sagas';


const persistConfig = {
    key: 'root',
    storage: storage,
    // whitelist: ['login', 'account', 'layout'],
    stateReconciler: autoMergeLevel2, // see "Merge Process" section for details.    
};

const persistedReducer = persistReducer(persistConfig, rootReducer);
const sagaMiddleware = createSagaMiddleware();
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;


const store = createStore(persistedReducer, composeEnhancers(applyMiddleware(sagaMiddleware)));
const persistor = persistStore(store);

export { store, persistor, sagaMiddleware };

reducers.js

import { combineReducers  } from 'redux';

// Front
import layout from './layout/reducer';

// Authentication Module
import account from './auth/register/reducer';
import login from './auth/login/reducer';
import forget from './auth/forgetpwd/reducer';

const rootReducer = combineReducers({
    layout,
    account,
    login,
    forget
});

export default rootReducer;

reducer.js (the one i'm testing, Layout)

import { ACTIVATE_AUTH_LAYOUT, ACTIVATE_NON_AUTH_LAYOUT, TOGGLE, TOGGLE_LD } from './actionTypes';

const initialState={
    topbar:true,
    sidebar:true,
    footer:true,
    is_toggle : true,
    is_light : true
}

const layout = (state=initialState,action) => {
    switch(action.type){
        case ACTIVATE_AUTH_LAYOUT:
            state = {
                ...state,
                ...action.payload
            }
            break;
        case ACTIVATE_NON_AUTH_LAYOUT:
            state = {
                ...state,
                ...action.payload
            }
            break;

        case TOGGLE:
            state = {
                ...state,
                is_toggle : action.payload
            }
            break;

        case TOGGLE_LD:
            state = {
                ...state,
                is_light : action.payload
            }
            break;

        default:
            // state = state;
            break;
    }
    return state;
}

export default layout;

actions.js

import { ACTIVATE_AUTH_LAYOUT, ACTIVATE_NON_AUTH_LAYOUT, TOGGLE, TOGGLE_LD } from './actionTypes';

export const activateAuthLayout = () => {
    return {
        type: ACTIVATE_AUTH_LAYOUT,
        payload: {
            topbar: true,
            sidebar: true,
            footer: true,
            rodri: 'butta',
            layoutType: 'Auth'
        }
    }
}

export const activateNonAuthLayout = () => {
    return {
        type: ACTIVATE_NON_AUTH_LAYOUT,
        payload: {
            topbar: false,
            sidebar: false,
            footer: false,
            layoutType: 'NonAuth'
        }
    }
}

export const toggleSidebar = (is_toggle) => {
    return {
        type: TOGGLE,
            payload: is_toggle 

    }
}

export const toggleLightDark = (is_light) => {
    return {
        type: TOGGLE_LD,
            payload: is_light 

    }
}


index.js (APP)

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import * as serviceWorker from './serviceWorker';
import { BrowserRouter } from 'react-router-dom';
import { Provider } from 'react-redux';
import { PersistGate } from 'redux-persist/lib/integration/react';

import rootSaga from './store/sagas';

import {persistor, store, sagaMiddleware} from './store';

sagaMiddleware.run(rootSaga);



const app = (
    <Provider store={store}>
        <PersistGate loading={null} persistor={persistor}>
            <BrowserRouter>
                <App />
            </BrowserRouter>
        </PersistGate>
    </Provider>
);

ReactDOM.render(app, document.getElementById('root'));
serviceWorker.unregister();

And fragments of the class

...
this.props.toggleSidebar(!this.props.is_toggle);
...

const mapStatetoProps = state => {
    const { is_toggle,is_light } = state.layout;
    return {  is_toggle,is_light };
}

export default withRouter(connect(mapStatetoProps, { toggleSidebar })(Topbar));

The debugger

First time run, when i toggle the menu bar so the action gets triggered)

After refreshing the browser, the rehydrate should bring the layout -> is_toggle to false.. but it remains true (as default)

Fragments in color


回答1:


I think your issue is how you are setting up your store:

const store = createStore(persistedReducer, composeEnhancers(applyMiddleware(sagaMiddleware)));

The structure of this function is createStore(reducer, [preloadedState], [enhancer])

So you are trying to pass your enhancers as preloadedState.

Try changing it to this:

const store = createStore(persistedReducer, {}, composeEnhancers(applyMiddleware(sagaMiddleware)));

Also, when you setup your persistConfig I see that you have the whitelist commented out. Any reducer state you want to keep needs to be in this list so it should not be commented out:

// whitelist: ['login', 'account', 'layout'],  // uncomment this

And finally as a side note, you don't need break in all of your switch cases



来源:https://stackoverflow.com/questions/59648985/react-redux-persist-is-rehydrating-but-no-stored-data-is-retrieved-instead-de

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!