2

React router works normal. But if I add <Redirect> or call from <Link>, I have this exception:

Uncaught Could not find router reducer in state tree, it must be mounted under "router"

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

1 Answers1

14

Check the history documentation. If you're using react-router-dom@5.x.x then you should use history@4.10.1 because the latest version of history (v5) only works with react-router-dom@6.x.x

  • had this issue, thought yarn automatically handles version installations base from installed dependencies. thanks for this – Semi-Friends Aug 29 '20 at 18:11