gpt4 book ai didi

reactjs - 未捕获在状态树中找不到路由器 reducer ,它必须安装在 "router"下

转载 作者:行者123 更新时间:2023-12-05 08:20:04 25 4
gpt4 key购买 nike

React 路由器工作正常。但是如果我添加 <Redirect>或调用<Link> ,我有这个异常(exception):

未捕获在状态树中找不到路由器 reducer ,它必须安装在“路由器”下

rootReducer.js:

    import { combineReducers } from 'redux';
import { connectRouter } from 'connected-react-router';
import counterReducer from './Counter/counter.reducer';
import sidebarReducer from './Sidebar/sidebar.reducer';

export default (history) => combineReducers({
router: connectRouter(history),
counter: counterReducer,
sidebar: sidebarReducer,
});

store.js:

    import { createBrowserHistory } from 'history';
import { applyMiddleware, compose, createStore } from 'redux';
import { routerMiddleware } from 'connected-react-router';
import createRootReducer from './rootReducer';

export const history = createBrowserHistory();

export default function configureStore(preloadedState) {
const composeEnhancer = compose
const store = createStore(
createRootReducer(history),
preloadedState,
composeEnhancer(
applyMiddleware(
routerMiddleware(history),
),
),
);

if (module.hot) {
module.hot.accept('./rootReducer', () => {
store.replaceReducer(createRootReducer(history));
});
}

console.log(store.getState());

return store;
}

最佳答案

检查 history文档。如果您使用的是 react-router-dom@5.x.x,那么您应该使用 history@4.10.1,因为最新版本的 history (v5) 仅适用于 react-router-dom@6.x.x

关于reactjs - 未捕获在状态树中找不到路由器 reducer ,它必须安装在 "router"下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62553781/

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