Gathering detailed insights and metrics for redux-router
Gathering detailed insights and metrics for redux-router
Gathering detailed insights and metrics for redux-router
Gathering detailed insights and metrics for redux-router
react-router-redux
Ruthlessly simple bindings to keep react-router and redux in sync
@types/react-router-redux
TypeScript definitions for react-router-redux
connected-react-router
A Redux binding for React Router v4 and v5
lit-redux-router
Declarative way of routing for lit powered by pwa-helpers and redux
Redux bindings for React Router – keep your router state inside your Redux store
npm install redux-router
Typescript
Module System
Node Version
NPM Version
Another version comes along! (1.0.0-beta6)
Updated on Jan 02, 2016
v1.0.0-beta5
Updated on Dec 04, 2015
getRoutes() is back
Updated on Sep 17, 2015
Dynamic Route Configuration or: How I Learned to Stop Worrying and Love Circular Dependencies
Updated on Sep 16, 2015
Fix extra dispatch after external state change
Updated on Jul 13, 2015
JavaScript (100%)
Total Downloads
4,083,884
Last Day
267
Last Week
1,376
Last Month
8,181
Last Year
98,110
MIT License
2,296 Stars
244 Commits
200 Forks
50 Watchers
3 Branches
40 Contributors
Updated on Apr 06, 2025
Minified
Minified + Gzipped
Latest Version
2.1.2
Package Id
redux-router@2.1.2
Size
276.37 kB
NPM Version
3.10.3
Node Version
6.3.1
Cumulative downloads
Total Downloads
Last Day
161.8%
267
Compared to previous day
Last Week
-43.2%
1,376
Compared to previous week
Last Month
-3.1%
8,181
Compared to previous month
Last Year
-45.6%
98,110
Compared to previous year
1
Redux bindings for React Router.
<Link />
, router.transitionTo()
, etc. still work.1# installs version 2.x.x 2npm install --save redux-router
Install the version 1.0.0 via the previous
tag
1npm install --save redux-router@previous
React Router is a fantastic routing library, but one downside is that it abstracts away a very crucial piece of application state — the current route! This abstraction is super useful for route matching and rendering, but the API for interacting with the router to 1) trigger transitions and 2) react to state changes within the component lifecycle leaves something to be desired.
It turns out we already solved these problems with Flux (and Redux): We use action creators to trigger state changes, and we use higher-order components to subscribe to state changes.
This library allows you to keep your router state inside your Redux store. So getting the current pathname, query, and params is as easy as selecting any other part of your application state.
1import React from 'react'; 2import { combineReducers, applyMiddleware, compose, createStore } from 'redux'; 3import { reduxReactRouter, routerStateReducer, ReduxRouter } from 'redux-router'; 4import { createHistory } from 'history'; 5import { Route } from 'react-router'; 6 7// Configure routes like normal 8const routes = ( 9 <Route path="/" component={App}> 10 <Route path="parent" component={Parent}> 11 <Route path="child" component={Child} /> 12 <Route path="child/:id" component={Child} /> 13 </Route> 14 </Route> 15); 16 17// Configure reducer to store state at state.router 18// You can store it elsewhere by specifying a custom `routerStateSelector` 19// in the store enhancer below 20const reducer = combineReducers({ 21 router: routerStateReducer, 22 //app: rootReducer, //you can combine all your other reducers under a single namespace like so 23}); 24 25// Compose reduxReactRouter with other store enhancers 26const store = compose( 27 applyMiddleware(m1, m2, m3), 28 reduxReactRouter({ 29 routes, 30 createHistory 31 }), 32 devTools() 33)(createStore)(reducer); 34 35 36// Elsewhere, in a component module... 37import { connect } from 'react-redux'; 38import { push } from 'redux-router'; 39 40connect( 41 // Use a selector to subscribe to state 42 state => ({ q: state.router.location.query.q }), 43 44 // Use an action creator for navigation 45 { push } 46)(SearchBox);
You will find a server-rendering example in the repo´s example directory.
redux-router will notice if the router state in your Redux store changes from an external source other than the router itself — e.g. the Redux Devtools — and trigger a transition accordingly!
react-router-redux (formerly redux-simple-router) takes a different approach to integrating routing with redux. react-router-redux lets React Router do all the heavy lifting and syncs the url data to a history location object in the store. This means that users can use React Router's APIs directly and benefit from the wide array of documentation and examples there.
The README for react-router-redux has a useful picture included here:
redux (store.routing
) ↔ react-router-redux ↔ history (history.location
) ↔ react-router
This approach, while simple to use, comes with a few caveats:
react-router-redux encourages users to use props directly from React Router in the components (they are passed down to any rendered route components). This means that if you want to access the location data far down the component tree, you may need to pass it down or use React's context feature.
This project, on the other hand takes the approach of storing the entire React Router data inside the redux store. This has the main benefit that it becomes impossible for the properties passed down by redux-router to the components in the Route to differ from the data included in the store. Therefore feel free to connect the router data to any component you wish. You can also access the route params from the store directly. redux-router also provides an API for hot swapping the routes from the Router (something React Router does not currently provide).
The picture of redux-router would look more like this:
redux (store.router
) ↔ redux-router ↔ react-router (via RouterContext)
This approach, also has its set of limitations:
Ultimately, your choice in the library is up to you and your project's needs. react-router-redux will continue to have a larger support in the community due to its inclusion into the reactjs github organization and visibility. react-router-redux is the recommended approach for react-router and redux integration. However, you may find that redux-router aligns better with your project's needs. redux-router will continue to be mantained as long as demand exists.
reduxReactRouter({ routes, createHistory, routerStateSelector })
A Redux store enhancer that adds router state to the store.
routerStateReducer(state, action)
A reducer that keeps track of Router state.
<ReduxRouter>
A component that renders a React Router app using router state from a Redux store.
push(location)
An action creator for history.push()
. mjackson/history/docs/GettingStarted.md#navigation
Basic example (let say we are at http://example.com/orders/new
):
1dispatch(push('/orders/' + order.id));
Provided that order.id
is set and equals 123
it will change browser address bar to http://example.com/orders/123
and appends this URL to the browser history (without reloading the page).
A bit more advanced example:
1dispatch(push({ 2 pathname: '/orders', 3 query: { filter: 'shipping' } 4}));
This will change the browser address bar to http://example.com/orders?filter=shipping
.
NOTE: clicking back button will change address bar back to http://example.com/orders/new
but will not change page content
replace(location)
An action creator for history.replace()
. mjackson/history/docs/GettingStarted.md#navigation
Works similar to the push
except that it doesn't create new browser history entry.
NOTE: Referring to the push
example: clicking back button will change address bar back to the URL before http://example.com/orders/new
and will change page content.
go(n)
goBack()
goForward()
1// Go back to the previous entry in browser history. 2// These lines are synonymous. 3history.go(-1); 4history.goBack(); 5 6// Go forward to the next entry in browser history. 7// These lines are synonymous. 8history.go(1); 9history.goForward();
@joshgeller threw together a good example on how to handle user authentication via a higher order component. Check out joshgeller/react-redux-jwt-auth-example
This library pairs well with redux-rx to trigger route transitions in response to state changes. Here's a simple example of redirecting to a new page after a successful login:
1const LoginPage = createConnector(props$, state$, dispatch$, () => { 2 const actionCreators$ = bindActionCreators(actionCreators, dispatch$); 3 const push$ = actionCreators$.map(ac => ac.push); 4 5 // Detect logins 6 const didLogin$ = state$ 7 .distinctUntilChanged(state => state.loggedIn) 8 .filter(state => state.loggedIn); 9 10 // Redirect on login! 11 const redirect$ = didLogin$ 12 .withLatestFrom( 13 push$, 14 // Use query parameter as redirect path 15 (state, push) => () => push(state.router.query.redirect || '/') 16 ) 17 .do(go => go()); 18 19 return combineLatest( 20 props$, actionCreators$, redirect$, 21 (props, actionCreators) => ({ 22 ...props, 23 ...actionCreators 24 }); 25});
A more complete example is forthcoming.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 6/15 approved changesets -- score normalized to 4
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-05-05
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More