|
| 1 | +React Router in a Flux App |
| 2 | +========================== |
| 3 | + |
| 4 | +```js |
| 5 | +/////////////////////////////////////////////////////////// |
| 6 | +// router.js |
| 7 | +var routes = require('./routes'); |
| 8 | +var Router = require('react-router'); |
| 9 | + |
| 10 | +// we can create a router instance before "running" it |
| 11 | +var router = Router.create({ |
| 12 | + routes: routes, |
| 13 | + location: Router.HistoryLocation |
| 14 | +}); |
| 15 | + |
| 16 | +module.exports = router; |
| 17 | + |
| 18 | +/////////////////////////////////////////////////////////// |
| 19 | +//SomeActions.js |
| 20 | + |
| 21 | +// and then action creators can require it like other |
| 22 | +// singletons in the app (or if you don't use singletons, |
| 23 | +// do something else with the router you created in |
| 24 | +// router.js) |
| 25 | +var router = require('../router'); |
| 26 | + |
| 27 | +module.exports = { |
| 28 | + doSomeTransitionThing: function () { |
| 29 | + router.transitionTo('somewhere'); |
| 30 | + } |
| 31 | +}; |
| 32 | + |
| 33 | +/////////////////////////////////////////////////////////// |
| 34 | +// main.js |
| 35 | + |
| 36 | +// finally, run it in your main script |
| 37 | +var React = require('react'); |
| 38 | +var router = require('./router'); |
| 39 | +router.run(function (Handler, state) { |
| 40 | + // you might want to push the state of the router to a |
| 41 | + // store for whatever reason |
| 42 | + RouterActions.routeChange({routerState: state}); |
| 43 | + React.render(<Handler/>, document.body); |
| 44 | +}); |
| 45 | +``` |
| 46 | + |
0 commit comments