Package Exports
- inferno-router
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (inferno-router) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
inferno-router
Inferno Router is a routing library for Inferno. It is a port of react-router 4.
Install
npm install inferno-routerFeatures
Same as react-router v4, except react-native support which we have tested at this point.
See official react-router documentation
Usage (client-side)
import { render } from 'inferno';
import { BrowserRouter, Route, Link } from 'inferno-router';
const Home = () => (
<div>
<h2>Home</h2>
</div>
);
const About = () => (
<div>
<h2>About</h2>
</div>
);
const Topic = ({ match }) => (
<div>
<h3>{match.params.topicId}</h3>
</div>
);
const Topics = ({ match }) => (
<div>
<h2>Topics</h2>
<ul>
<li>
<Link to={`${match.url}/rendering`}>
Rendering with React
</Link>
</li>
<li>
<Link to={`${match.url}/components`}>
Components
</Link>
</li>
<li>
<Link to={`${match.url}/props-v-state`}>
Props v. State
</Link>
</li>
</ul>
<Route path={`${match.url}/:topicId`} component={Topic}/>
<Route exact path={match.url} render={() => (
<h3>Please select a topic.</h3>
)}/>
</div>
);
const MyWebsite = () => (
<BrowserRouter>
<div>
<ul>
<li><Link to="/">Home</Link></li>
<li><Link to="/about">About</Link></li>
<li><Link to="/topics">Topics</Link></li>
</ul>
<hr/>
<Route exact path="/" component={Home}/>
<Route path="/about" component={About}/>
<Route path="/topics" component={Topics}/>
</div>
</BrowserRouter>
);
// Render HTML on the browser
render(<MyWebsite />, document.getElementById('root'));Usage (server) with express
First, let's create our component to render boilerplate HTML, header, body etc.
// Routes will be rendered into children
export default function Html({ children }) {
return (
<html>
<head>
<title>My Application</title>
</head>
<body>
<div id="root">{children}</div>
</body>
</html>
);
}import { renderToString } from 'inferno-server';
import { StaticRouter } from 'inferno-router';
import express from 'express';
import Html from './Html';
const app = express();
app.use((req, res) => {
const renderProps = match(routes, req.originalUrl);
if (renderProps.redirect) {
return res.redirect(renderProps.redirect)
}
const context = {};
const content = renderToString(
<StaticRouter location={ctx.url} context={context}>
<Html/>
</StaticRouter>
);
res.send('<!DOCTYPE html>\n' + renderToString(content));
});Usage (server) with koa v2
import { renderToString } from 'inferno-server';
import { StaticRouter } from 'inferno-router';
import Koa from 'koa';
import Html from './Html';
const app = new Koa();
app.use(async(ctx, next) => {
const context = {};
const content = renderToString(
<StaticRouter location={ctx.url} context={context}>
<Html/>
</StaticRouter>
);
// This will contain the URL to redirect to if <Redirect> was used
if (context.url) {
return ctx.redirect(context.url)
}
ctx.body = '<!DOCTYPE html>\n' + content;
await next();
});Differences with React-Router v4
- No "official" react-native support.
- There's no
inferno-router-dom, all functionality is insideinferno-router