don't use it yet :)
A fork of connected-react-router to support react-router v6 with reduced API surface.
Originally created by Supasate Choochaisri
-
Synchronize router state with redux store through uni-directional flow (i.e. history -> store -> router -> components).
-
Supports React Router v6.
-
Dispatching of history methods (
push,replace,go,back,forward) works for both redux-thunk and redux-saga. -
Supports time traveling in Redux DevTools.
-
Supports TypeScript
- Does not support immutable.js and seamless-immutable
- Does not support react-router v4, v5
- Does not support history v4
Connected React Router requires React 17 and React Redux 7.1 or later.
$ npm install --save connected-react-router-lite
Or
$ yarn add connected-react-router-lite
In your root reducer file,
- Create a function that takes
historyas an argument and returns a root reducer. - Add
routerreducer into root reducer by passinghistorytocreateRouterReducer. - Note: The key MUST be
'router'.
// reducers.js
import { combineReducers } from "redux";
import { createRouterReducer } from "connected-react-router-lite";
import { History } from "history";
export const createRootReducer = (history: History) => combineReducers({
router: createRouterReducer(history),
... // rest of your reducers
});When creating a Redux store,
- Create a
historyobject. - Provide the created
historyto the root reducer creator. - Use
routerMiddleware(history)if you want to dispatch history actions (e.g. to change URL withpush('/path/to/somewhere')).
// configureStore.js
...
import { createBrowserHistory } from "history";
import { applyMiddleware, compose, createStore } from "redux";
import { routerMiddleware } from "connected-react-router-lite";
import { createRootReducer } from "./reducers";
...
export const history = createBrowserHistory();
export function configureStore(preloadedState) {
const rootReducer = createRootReducer(history);
const store = createStore(
rootReducer,
preloadedState,
compose(
applyMiddleware(
routerMiddleware(history), // for dispatching history actions
// ... other middlewares ...
),
),
);
return store;
}- Wrap your react-router v6 routing with
ConnectedRouterand pass thehistoryobject as a prop. - Place
ConnectedRouteras a child ofreact-redux'sProvider. - N.B. If doing server-side rendering, you should still use the
StaticRouterfromreact-routeron the server.
// index.js
...
import { Provider } from "react-redux";
import { Route, Routes } from "react-router";
import { ConnectedRouter } from "connected-react-router-lite";
import { configureStore, history } from "./configureStore";
...
const store = configureStore();
ReactDOM.render(
<Provider store={store}>
<ConnectedRouter history={history}>
<>
<Routes>
<Route exact path="/" render={<div>Match</div>} />
<Route element={<div>Miss</div>} />
</Routes>
</>
</ConnectedRouter>
</Provider>,
document.getElementById("react-root"),
);Note: the history object provided to router reducer, routerMiddleware, and ConnectedRouter component must be the same history object.
Now, it's ready to work!
yarn buildGenerated files will be in the lib folder.
When testing the example apps with npm link or yarn link, you should explicitly provide the same Context to both Provider and ConnectedRouter to make sure that the ConnectedRouter doesn't pick up a different ReactReduxContext from a different node_modules folder.
In index.js.
...
import { Provider, ReactReduxContext } from 'react-redux'
...
<Provider store={store} context={ReactReduxContext}>
<App history={history} context={ReactReduxContext} />
</Provider>
...In App.js,
...
const App = ({ history, context }) => {
return (
<ConnectedRouter history={history} context={context}>
{ routes }
</ConnectedRouter>
)
}
...See Contributors and Acknowledge.