redux-universal-router
A universal router designed for redux.
- works server and client-side
- keeps the router state in your redux store
- fetch async data or
require.ensure
via redux actions
Highly inspired by fluxible-router, it uses routr and history.
This is a work-in progess
Not ready for production: redux-universal-router is a baby-project, there's still work to be done, especially on the client-side part.
Usage
You can run an example from the example directory:
git clone https://github.com/gpbl/redux-universal-router.gitcd redux-universal-routercd examplenpm installnpm start# now open http://localhost:3000
Steps
- Define the routes as objects
- Set up the reducer and the redux store
- Server-side: create a router instance and render the root component
- Client-side: create a router instance, listen to history and mount the root component
- Connect the root component to the router
- Use
<Link>
or dispatchROUTE_NAVIGATE
to navigate between routes
1. Define the routes as objects
Routes are the same objects you pass to routr, with two additional parameters:
handler
(required) is the react component that should will render the routeactionCreator
(optional) is a redux action creator that will be dispatched before the route is rendered- it will receive the route's params as the only argument
- the action returned by the action creator must follow the FSA standard
- if the
payload
returned by the action is aPromise
, the route will wait for it before navigating to the new route
const routes = home: path: "/" method: "get" // remember this is required handler: HomePage photo: path: "/photo/:id" method: "get" handler: PhotoPage actionCreator: requestPhoto ;
2. Set up the reducer and the redux store
To work correctly, the router's reducer must save its data in the router
key
in the root of the store:
// add the router reducer;const reducer = ; // create the redux store;const store = ;
3. Server-side: create a router instance and render the root component
Use router.navigate(url, callback)
to render the root component.
Remember you must create a new store and a new router for each request. So your middleware could look like:
;;; { const store = ; const router = store routes ; router;};
4. Client-side: create a router instance, listen to history and mount the root component
;;; const store = ;const router = store routes ; // listen to browser historyrouter; router
5. Connect the root component to the router
Since the router state is saved in the store, you can just connect
your
root component and use its data to know which route handler should be rendered.
The router's store has three parameters: currentRoute
, nextRoute
and err
, all optionals.
In your root component, you want to render the handler
component of the currentRoute
.
The component is available as currentRoute.config.handler
.
- use
currentRoute
to know get the config of the current route. Careful: if the route is not available in yourroutes
, this will be null. - use
nextRoute
to get the config of the route it's being called. It has a value only while navigating to a new route, e.g. when waiting to fetch data from an external API. - use
err
to know if you have to display an error when loading the route. The route'sactionCreator
can return an error with astatusCode
, to render an error page or a "not-found" page.
;;; @ { const router = thisprops; const currentRoute nextRoute err = router; const Handler = currentRoute && currentRouteconfighandler; return <div> err && errstatusCode === 404 && <NotFoundPage /> err && errstatusCode !== 404 && <ErrorPage error= err /> !err && <Handler ...currentRouteparams /> </div> ; }
<Link>
or dispatch ROUTE_NAVIGATE
to navigate between routes
6. Use Link
is the included React component to replace your <a>
:
; { return <p> <Link href="/route">click me</Link> </p> }
Connecting the component to the store, you can also dispatch ROUTE_NAVIGATE
to
navigate to another url:
; @ { return <p> <a onClick= thishandleClick >click me too</a> </p> } { e; thisprops }