简体   繁体   中英

Redux-persist integration issue

I want to keep my state data as it is even after browser refresh. For that i'm using Redux Persist ( https://www.npmjs.com/package/redux-persist#basic-usage ) library. When implementing this library i'm getting undefined for the current states in the reducers. For ex:

TypeError: Cannot read property 'modalType' of undefined

What am i doing wrong in the following code

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
//import * as serviceWorker from './serviceWorker';
import { createStore, applyMiddleware } from 'redux';
import { Provider } from 'react-redux';
import { composeWithDevTools } from 'redux-devtools-extension';
import thunk from 'redux-thunk';
import { routerMiddleware } from 'react-router-redux';
import ReducerStore from './reducers/rootReducer.js';
import '../node_modules/bootstrap/dist/css/bootstrap.min.css';
import { Router } from 'react-router-dom';
import history from './history';
import { persistStore, persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage';

const persistConfig = {
    key: 'root',
    storage,
}
const persistedReducer = persistReducer(persistConfig, ReducerStore)

const middleware = routerMiddleware('');

const store = createStore(persistedReducer, composeWithDevTools(
    applyMiddleware(middleware, thunk),
))

const persistor = persistStore(store);

ReactDOM.render(<Provider store={persistor}><Router history={history}><App /></Router></Provider>, document.getElementById('root'));

I was missing PersistGate . Once adding it i was able to make it work

import {PersistGate} from 'redux-persist/integration/react'

and then

ReactDOM.render(<Provider store={store}><PersistGate loading={null} persistor={persistor}><Router history={history}><App /></Router></PersistGate></Provider>, document.getElementById('root'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM