提交 39e575ac authored 作者: vipcxj's avatar vipcxj

解决redux-persist的hmr无限加载问题

上级 68d605b2
......@@ -2,8 +2,8 @@
import dva from 'dva';
import createLoading from 'dva-loading';
import moment from 'moment';
// import { REHYDRATE } from 'redux-persist/lib/constants';
import persistReducer from 'redux-persist/lib/persistReducer';
import persistStore from 'redux-persist/lib/persistStore';
import storage from 'redux-persist/lib/storage';
import { getHistory } from './services/route';
import { initApp } from './data/app';
......@@ -17,17 +17,16 @@ moment.locale('zh-cn');
const persistConfig = {
key: 'root',
debug: process.env.NODE_ENV === 'development',
storage,
};
const persistEnhancer = () => createStore => (reducer, initialState, enhancer) => createStore(persistReducer(persistConfig, reducer), initialState, enhancer);
// 1. Initialize
const app = dva({
history: getHistory({
basename: config.basename,
}),
extraEnhancers: [persistEnhancer()],
onReducer: reducer => persistReducer(persistConfig, reducer),
onError(error) {
// eslint-disable-next-line no-console
processError(error);
......@@ -53,6 +52,7 @@ initApp(app).then(theApp => theApp.start('#root'));
export default app;
export const getStore = () => {
// eslint-disable-next-line no-underscore-dangle
return app._store;
};
export const createPersistor = theApp => persistStore(theApp._store);
......@@ -4,9 +4,7 @@ import PropTypes from 'prop-types';
import { Router4Compat as Router } from 'react-router-4-compat';
import { LocaleProvider, Spin } from 'antd';
import zhCN from 'antd/lib/locale-provider/zh_CN';
import persistStore from 'redux-persist/lib/persistStore';
import { PersistGate } from 'redux-persist/integration/react';
import { REHYDRATE } from 'redux-persist/lib/constants';
import isString from 'lodash/isString';
import config from './utils/config';
import { getUser, isAuthed, hasDomain, histories } from './utils/auth';
......@@ -15,9 +13,9 @@ import App from './routes/app';
import { getMenus, getModuleInfo, getModuleLayout } from './data/modules';
import { bindModel } from './utils/model';
import Monk from './routes/main/monk';
import { createPersistor } from './index';
import styles from './index.css';
import { getStore } from './index';
const Loading = <Spin size="large" className={styles.globalSpin} />;
......@@ -309,7 +307,7 @@ function RouterConfig({ history, app }) {
});
}
return (
<PersistGate persistor={persistStore(app._store)} loading={Loading}>
<PersistGate persistor={createPersistor(app)} loading={Loading}>
<LocaleProvider locale={zhCN}>
<Router history={history} routes={routes} onError={processError} />
</LocaleProvider>
......@@ -323,9 +321,3 @@ RouterConfig.propTypes = {
};
export default RouterConfig;
if (module.hot) {
module.hot.accept(() => {
getStore().dispatch({ type: REHYDRATE });
});
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论