When setting up your Redux store:
- Create a
history object (e.g., using createBrowserHistory from the history package). - Pass this
history object to your root reducer creator. - Use
routerMiddleware(history) in your middleware configuration to enable dispatching history actions (like push('/path')) from Redux actions.
// configureStore.js
import { createBrowserHistory } from 'history'
import { applyMiddleware, compose, createStore } from 'redux'
import { routerMiddleware } from 'connected-react-router'
import createRootReducer from './reducers'
export const history = createBrowserHistory()
export default function configureStore(preloadedState) {
const store = createStore(
createRootReducer(history), // root reducer with router state
preloadedState,
compose(
applyMiddleware(
routerMiddleware(history), // for dispatching history actions
// ... other middlewares ...
),
),
)
return store
}