redux-little-router
redux-little-router
is a tiny router for Redux applications that lets the URL do the talking.
The router follows three basic principles:
- The URL is just another member of the state tree.
- URL changes are just plain actions.
- Route matching should be simple and extendable.
While the core router does not depend on any view library, it provides flexible React bindings and components.
Why another router?
To understand why redux-little-router
exists, check out our blog series, "Let the URL do the Talking":
Part 1
Part 2
Part 3
While React Router is a great, well-supported library, it hoards URL state within the view layer and makes certain Redux patterns difficult, if not impossible. This chart outlines a major issue in accessing URL state from outside of React Router.
react-router-redux
is meant only to enable time-travel debugging in React Router and doesn't allow you to safely access URL state. redux-router
, while allowing you to access URL state, is experimental, lags behind React Router releases, and recommends react-router-redux
in its README
.
redux-little-router
makes URL state a first-class citizen of your Redux store and abstracts cross-browser navigation and routing into a pure Redux API.
Redux usage
To hook into Redux applications, redux-little-router
uses a store enhancer that wraps the history
module and adds current and previous router state to your store. The enhancer listens for location changes and dispatches rich actions containing the URL, parameters, and any custom data assigned to the route. redux-little-router
also adds a middleware that intercepts navigation actions and calls their equivalent method in history
.
Wiring up the boilerplate
The following is an example of a redux-little-router
setup that works for browser-rendered applications. For a server rendering example, check out our advanced docs.
import { combineReducers, compose, createStore, applyMiddleware } from 'redux';
import { routerForBrowser } from 'redux-little-router';
import yourReducer from './your-app';
const routes = {
'/messages': {
title: 'Message'
},
'/messages/:user': {
title: 'Message History'
},
'/': {
title: 'Home',
'/bio': {
title: 'Biographies',
'/:name': {
title: 'Biography for:'
}
}
}
};
const { reducer, middleware, enhancer } = routerForBrowser({
routes,
basename: '/example'
});
const clientOnlyStore = createStore(
combineReducers({ router: reducer, yourReducer }),
initialState,
compose(enhancer, applyMiddleware(middleware))
);
Often, you'll want to update state or trigger side effects after loading the initial URL. To maintain compatibility with other store enhancers (particularly ones that handle side effects, like redux-loop
or redux-saga
), we require this optional initial dispatch to happen in client code by doing the following:
import { initializeCurrentLocation } from 'redux-little-router';
const initialLocation = store.getState().router;
if (initialLocation) {
store.dispatch(initializeCurrentLocation(initialLocation));
}
Provided actions and state
redux-little-router
provides the following action creators for navigation:
import { push, replace, go, goBack, goForward } from 'redux-little-router';
push('/messages');
push('/messages?filter=business');
replace({
pathname: '/messages',
query: {
filter: 'business'
}
});
push(
{
pathname: '/messages',
query: {
filter: 'business'
}
},
{
persistQuery: true
}
);
go(3);
go(-6);
goBack();
goForward();
block((location, action) => {
if (location.pathname === '/messages') {
return 'Are you sure you want to leave the messages view?';
}
});
unblock();
Note: if you used the vanilla action types prior to v13
, you'll need to migrate to using the public action creators.
These actions will execute once dispatched. For example, here's how to redirect using a thunk:
import { push } from 'redux-little-router';
export const redirect = href => dispatch => {
dispatch(push(href));
};
On location changes, the store enhancer dispatches a LOCATION_CHANGED
action that contains at least the following properties:
{
pathname: '/messages/a-user-has-no-name',
route: '/messages/:user',
params: {
user: 'a-user-has-no-name'
},
query: {
some: 'thing'
},
search: '?some=thing',
result: {
arbitrary: 'data that you defined in your routes object!'
parent: {
}
}
}
Your custom middleware can intercept this action to dispatch new actions in response to URL changes.
The reducer consumes this action and adds the following to the root of the state tree on the router
property:
{
pathname: '/messages/a-user-has-no-name',
route: '/messages/:user',
params: {
user: 'a-user-has-no-name'
},
query: {
some: 'thing'
},
search: '?some=thing',
result: {
arbitrary: 'data that you defined in your routes object!',
parent: { },
},
previous: {
pathname: '/messages',
route: '/messages',
params: {},
query: {},
result: {
more: 'arbitrary data that you defined in your routes object!'
parent: { }
}
}
}
Your custom reducers or selectors can derive a large portion of your app's state from the URLs in the router
property.
React bindings and usage
redux-little-router
provides the following to make React integration easier:
- A
<Fragment>
component that conditionally renders children based on current route and/or location conditions. - A
<Link>
component that sends navigation actions to the middleware when tapped or clicked. <Link>
respects default modifier key and right-click behavior. A sibling component, <PersistentQueryLink>
, persists the existing query string on navigation.
Instances of each component automatically connect()
to the router state with react-redux
.
You can inspect the router state in any child component by using connect()
:
const mapStateToProps = state => ({ router: state.router });
export default connect(mapStateToProps)(YourComponent);
<Fragment>
Think of <Fragment>
as the midpoint of a "flexibility continuum" that starts with raw switch statements and ends with React Router v3's <Route>
component. Fragments can live anywhere within the React tree, making split-pane or nested UIs easy to work with.
The simplest fragment is one that displays when a route is active:
<Fragment forRoute="/home/messages/:team">
<p>This is the team messages page!</p>
</Fragment>
You can also match a fragment against anything in the current location
object:
<Fragment withConditions={location => location.query.superuser}>
<p>Superusers see this on all routes!</p>
</Fragment>
You can use withConditions
in conjunction with forRoute
to set strict conditions for when a <Fragment>
should display.
To show a Fragment
when no other Fragment
s match a route, use <Fragment forNoMatch />
.
<Fragment>
lets you nest fragments to match your UI hierarchy to your route hierarchy, much like the <Route>
component does in react-router@v3
. Given a URL of /about/bio/dat-boi
, and the following elements:
<Fragment forRoute="/about">
<div>
<h1>About</h1>
<Fragment forRoute="/bio">
<div>
<h2>Bios</h2>
<Fragment forRoute="/dat-boi">
<div>
<h3>Dat Boi</h3>
<p>Something something whaddup</p>
</div>
</Fragment>
</div>
</Fragment>
</div>
</Fragment>
...React will render:
<div>
<h1>About</h1>
<div>
<h2>Bios</h2>
<div>
<h3>Dat Boi</h3>
<p>Something something whaddup<p>
</div>
</div>
</div>
<Fragment>
makes basic component-per-page navigation easy:
<Fragment forRoute="/">
<div>
<Fragment forRoute="/">
<Home />
</Fragment>
<Fragment forRoute="/about">
<About />
</Fragment>
<Fragment forRoute="/messages">
<Messages />
</Fragment>
<Fragment forRoute="/feed">
<Feed />
</Fragment>
</div>
</Fragment>
<Link>
Using the <Link>
component is simple:
<Link className="anything" href="/yo">
Share Order
</Link>
Alternatively, you can pass in a location object to href
. This is useful for passing query objects:
<Link
className="anything"
href={{
pathname: '/home/messages/a-team?test=ing',
query: {
test: 'ing'
}
}}
>
Share Order
</Link>
To change how <Link>
renders when its href
matches the current location (i.e. the link is "active"), use activeProps
. For example, you can add className
to activeProps
to use a different CSS class when the link is active:
<Link
href="/wat"
className="normal-link"
activeProps={{ className: 'active-link' }}
>
Wat
</Link>
<Link>
takes an optional valueless prop, replaceState
, that changes the link navigation behavior from pushState
to replaceState
in the History API.
Use with immutable
redux-little-router
supports the use of immutable.js in tandem with an immutable
-aware combineReducers
function like provided by redux-immutable
. To use it, you will need to import the immutable version of the router or component you want to use. For instance,
import { immutableRouterForBrowser, ImmutableLink } from 'redux-little-router/es/immutable';
import { combineReducers } from 'redux-immutable';
const { reducer, enhancer, middleware } = immutableRouterForBrowser({ routes });
const store = createStore(
combineReducers({ router: reducer, ... }),
...
);
Depending on your environment, you might need to modify the import statement further. In that case, here are some tips:
import { immutableRouterForBrowser } from 'redux-little-router/es/immutable';
import { immutableRouterForBrowser } from 'redux-little-router/lib/immutable';
import { immutableRouterForBrowser } from 'redux-little-router/immutable';
Environment
redux-little-router
requires an ES5 compatible environment (no IE8).
Stability
We consider redux-little-router
to be stable. Any API changes will be incremental.
Versioning
redux-little-router
follows strict semver. Don't be alarmed by the high version number! Major version bumps represent any breaking change, no matter how small, and do not represent a major shift in direction. We strive to make breaking changes small and compartmentalized.
Contributing
We welcome community contributions! We have standardized our dev experience on yarn
so make sure to have that installed.
$ git clone git@github.com:FormidableLabs/redux-little-router.git
$ cd redux-little-router
$ yarn install
After any changes and before a PR, make sure to pass our build and quality checks:
$ yarn run build
$ yarn run check
When ready for release, we use an npm version
workflow:
$ npm version <major|minor|patch|ACTUAL_VERSION_NUMBER>
$ npm publish
$ git push && git push --tags
After publishing, consider drafting some release notes to let the world know about all the great new features!