2

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

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

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,
    });

商店.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;
    }
4

1 回答 1

14

检查历史文档。如果你使用 react-router-dom@5.xx 那么你应该使用 history@4.10.1 因为最新版本的历史 (v5) 只适用于 react-router-dom@6.xx

于 2020-07-16T21:41:28.187 回答