Skip to content
This repository has been archived by the owner on Dec 15, 2018. It is now read-only.

Commit

Permalink
Update docs with new enhancer/middleware factory methods
Browse files Browse the repository at this point in the history
  • Loading branch information
tptee committed Oct 7, 2016
1 parent ac75fd5 commit c39662e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 19 deletions.
41 changes: 30 additions & 11 deletions ADVANCED.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@

Make sure to read http://redux.js.org/docs/recipes/ServerRendering.html to understand how the server/client Redux boilerplate works.

Here's what the setup looks like on the server:
Here's what the setup looks like on the server (assuming Node 4 LTS):

```js
const express = require('express');
const app = express();

const createStore = require('redux').createStore;
const routerForExpress = require('redux-little-router')
.routerForExpress;

const redux = require('redux');
const createStore = redux.createStore;
const compose = redux.compose;
const applyMiddleware = redux.applyMiddleware;

const routes = {
'/': {
'/whatever': {
Expand All @@ -24,19 +27,27 @@ const routes = {
};

app.use('/*', (req, res) => {
// Create the Redux store, passing in the
// Express request to the store enhancer.
// Create the Redux store, passing in the Express
// request to the routerForExpress factory.
//
// If you're using an Express sub-router,
// routerForExpress will infer the basename
// from req.baseUrl!
//
const router = routerForExpress({
routes,
request: req
})

const store = createStore(
state => state,
{ what: 'ever' },
routerForExpress({
routes,
request: req
})
compose(
router.routerEnhancer,
applyMiddleware(
router.routerMiddleware
)
)
);

// ...then renderToString() your components as usual,
Expand All @@ -51,7 +62,7 @@ app.use('/*', (req, res) => {
There's not much involved on the client side, post-server render:

```js
import { createStore } from 'redux';
import { createStore, compose, applyMiddleware } from 'redux';
import { routerForBrowser } from 'redux-little-router';

// The same routes that you used on the server.
Expand All @@ -64,10 +75,18 @@ const routes = {
}
};

const {
routerEnhancer,
routerMiddleware
} = routerForBrowser({ routes });

const store = createStore(
yourReducer,
window.__INITIAL_STATE,
routerForBrowser({ routes })
compose(
routerEnhancer,
applyMiddleware(routerMiddleware)
)
);

// ...then render() your components as usual,
Expand Down
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ While React Router is a great, well-supported library, it hoards URL state withi

## 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. It also intercepts navigation actions and calls their equivalent method in `history`.
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

Expand Down Expand Up @@ -67,16 +67,26 @@ const routes = {
}
};

// Install the router into the store for a browser-only environment
// Install the router into the store for a browser-only environment.
// routerForBrowser is a factory method that returns a store
// enhancer and a middleware.
const {
routerEnhancer,
routerMiddleware
} = routerForBrowser({
// The configured routes. Required.
routes,
// The basename for all routes. Optional.
basename: '/example'
})

const clientOnlyStore = createStore(
yourReducer,
initialState,
routerForBrowser({
// The configured routes. Required.
routes,
// The basename for all routes. Optional.
basename: '/example'
})
compose(
routerEnhancer,
applyMiddleware(routerMiddleware)
)
);
```

Expand Down

0 comments on commit c39662e

Please sign in to comment.