gpt4 book ai didi

reactjs - connectRouter错误: Could not find router reducer in state tree,它必须安装在 "router"下

转载 作者:行者123 更新时间:2023-12-03 13:18:45 28 4
gpt4 key购买 nike

我是 React.js 的新手,在设置基础项目时遇到一个问题,即我的路由已更改但组件未加载。谷歌搜索后我发现我需要使用 ConnectedRouter。设置 ConnectedRouter 时,我收到控制台错误:无法在状态树中找到路由器 reducer ,它必须安装在“路由器”下

import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom';
import { ConnectedRouter, connectRouter, routerMiddleware } from "connected-react-router";
import { Provider } from "react-redux";
import { createStore, combineReducers, applyMiddleware, compose } from 'redux';
import createSagaMiddleware from 'redux-saga';
import loginReducer from "./store/reducers/login";
import { watchLogin} from "./store/sagas";
import { history } from '../src/shared/history';
import { push } from 'react-router-redux';



import './index.css';
import App from './App';

const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

const rootReducer = combineReducers({
login: loginReducer
});
const routersMiddleware = routerMiddleware(history)
const sagaMiddleware = createSagaMiddleware();
const middlewares = [sagaMiddleware, routersMiddleware];

const store = createStore(
connectRouter(history)(rootReducer),
{},
composeEnhancers(applyMiddleware(...middlewares))
);

sagaMiddleware.run(watchLogin);

const app = (
<Provider store={store}>
<ConnectedRouter history={history}>
<App />
</ConnectedRouter>
</Provider>
);

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

最佳答案

为了帮助 future 的灵魂解决这个问题,事实证明,根据链接的 github 讨论,history 包的 5.0 版本导致了该问题并降级到版本 4.10.1为我解决了问题。

npm install history@4.10.1

https://github.com/ReactTraining/history/issues/803

https://github.com/ReactTraining/history/issues/804

关于reactjs - connectRouter错误: Could not find router reducer in state tree,它必须安装在 "router"下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54315988/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com