Sane Router (for React)
…because most routers suck at something. Sane Router supports:
-
location-dependent and location-independent (in-memory) routing
var locationRouter =var memoryRouter = -
pathname base (root)
var router =router// Routes /app/my/path as /my/path, thus to the "my-target" target.// Pathnames outside the /app namespace are ignored. -
multiple instances living side-by-side without interfering
var router1 =var router2 = -
plain React elements
router<App>router</App> -
route aliasing
router -
route parameters
routerrouter -
seamless error page registration, error page is just another target
routerrouterNote: The
router.StatusCodes.NOT_FOUND
target is used for missing routes. The target is, however, not registered by default androuter.createTargetElement()
is an empty<div>
element. -
React contexts as there are no inner
render()
callsComponentchildContextTypes: myService: ReactPropTypesobject{ myService }{ router }ComponentcontextTypes: myService: ReactPropTypesobject{ <span>thiscontextmyServicedata</span> }myService = data: 'Whoa'router// <App> renders <span>Whoa</span>
Installation
npm install react-sane-router
Usage
- Import the factory and create a router
router =
The default history manupulation driver
- Declare targets
These are React elements or React element factories. Targets can be nested.
routerrouter
- Declare routes
A route is a pathname mapping onto a target or a different pathname. Nested targets are referenced with target paths.
router
- Insert the target element to the render tree
This can be wherever desired in the tree, the following demonstrates insertion to the root component App
.
var { <div class="app">router</div>} var node = documentdocumentbodyReactDOM
Examples
Check out the examples/
directory.
You can build and run the examples on your machine:
git clone git://github.com/jankuca/react-sane-router.git
cd react-sane-router
npm install
npm run build-examples
open examples/single-router/index.html