我正在尝试根据反应样板 4 将 redux-persist 添加到我的 Web 应用程序中。我已按照文档进行操作,但收到错误无法在状态树中找到路由器减速器,它必须安装在“路由器”下。
实施的更改:
在 configureStore.js
import { createStore, applyMiddleware, compose } from 'redux';
import { persistStore, persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage'; // defaults to localStorage for web
import { routerMiddleware } from 'connected-react-router';
... Other imports...
const persistConfig = {
key: 'root',
storage,
};
const persistedReducer = persistReducer(persistConfig, createReducer);
export default function configureStore(initialState = {}, history) {
... Boilerplate code...
const sagaMiddleware = createSagaMiddleware(reduxSagaMonitorOptions);
const middlewares = [sagaMiddleware, routerMiddleware(history)];
const enhancers = [applyMiddleware(...middlewares)];
const store = createStore(
persistedReducer,
initialState,
composeEnhancers(...enhancers),
);
const persistor = persistStore(store);
... Boilerplate code...
return { store, persistor };
}
reducers.js 没有变化
在 app.js 中:
... Other imports...
import { PersistGate } from 'redux-persist/integration/react';
... Other imports...
const initialState = {};
const { store, persistor } = configureStore(initialState, history);
const MOUNT_NODE = document.getElementById('app');
const render = messages => {
ReactDOM.render(
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
<LanguageProvider messages={messages}>
<ConnectedRouter history={history}>
<App />
</ConnectedRouter>
</LanguageProvider>
</PersistGate>
</Provider>,
MOUNT_NODE,
);
};
...More code here...
我得到的完整错误是:
未捕获在状态树中找不到路由器减速器,它必须安装在“路由器”下
react-dom.development.js?61bb:17117 组件出现上述错误: in ConnectedRouter (created by Context.Consumer) in ConnectedRouterWithContext (created by ConnectFunction) in ConnectFunction in IntlProvider (created by LanguageProvider) in LanguageProvider (created by ConnectFunction ) 在 Provider 的 PersistGate 中的 ConnectFunction
考虑向树中添加错误边界以自定义错误处理行为。访问 fb.me/react-error-boundaries 以了解有关错误边界的更多信息。```