[英]Redux-persist integration issue
我想保留我的 state 數據,即使在瀏覽器刷新后也是如此。 為此,我正在使用Redux Persist
( https://www.npmjs.com/package/redux-persist#basic-usage ) 庫。 在實現這個庫時,我對減速器中的當前狀態沒有定義。 例如:
TypeError: Cannot read property 'modalType' of undefined
我在下面的代碼中做錯了什么
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'));
我錯過了PersistGate
。 一旦添加它,我就能讓它工作
import {PersistGate} from 'redux-persist/integration/react'
接着
ReactDOM.render(<Provider store={store}><PersistGate loading={null} persistor={persistor}><Router history={history}><App /></Router></PersistGate></Provider>, document.getElementById('root'));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.