inferno-router
Inferno Router is a routing library for Inferno.
Usage of inferno-router
is similar to that of react-router.
Install
npm install inferno-router
Features
- Router / RouterContext
- Route / IndexRoute
- Link / IndexLink
- Redirect / IndexRedirect
- browserHistory / memoryHistory
- onEnter / onLeave hooks
- onUpdate event support for Router
- params / querystring parsing
Usage (client-side)
;;; const browserHistory = ; { // ...} { // ...} { // ...} // `children` in this case will be the `User` component { return <div> children </div>} { return <div> JSON </div>} const routes = <Router history= browserHistory > <Route component= App > <IndexRoute component= Home /> <Route path="users" component= Users > <Route path="/user/:username" component= User /> </Route> <Route path="*" component= NoMatch /> </Route> </Router>; // Render HTML on the browserInferno;
Server-side rendering (express)
;;;; { return <html> <head> <title>My Application</title> </head> <body> <div id="root">children</div> </body> </html> ;} const app = ; app;
Server-side rendering (koa v2)
;;;; { return <html> <head> <title>My Application</title> </head> <body> <div id="root">children</div> </body> </html> ;} const app = app;
onEnter / onLeave hooks
In some cases, you may need to execute some logic before or after routing.
You can easily do this by passing a function
to the Route
component via a prop, as shown below:
;;; { // ...} { if !propsloggedIn router; } { } Inferno;
onUpdate event
In some cases you may need to execute some code on route updates. If so you can do this:
;;; { // ...} { // ...} Inferno;
Redirect
<Router history= > <Redirect from="/oldpath" to="/newpath"/> <Route path="/newpath" component= MyComponent /></Router>
Notes
<IndexRoute>
is the same as<Route path="/">"
<IndexLink>
is the same as<Link to="/">