Note: reactreact-dom & react-router are all peerDependencies of koa-react-router.
koa-react-router can be mounted easily in a koa 2 application like so:
// index.jsimportKoafrom'koa';importreactrouterfrom'koa-react-router';importAppfrom'./App';importContainerfrom'./containers/PageContainer';constapp=newKoa();app.use(reactrouter({
App,onError: (ctx,err)=>console.log('I Have failed!!!!'),onRedirect: (ctx,redirect)=>console.log('I have redirected!'),onRender: (ctx)=>({ Container })}));
API
koa-react-router requires the following parameters:
App
The App config prop should be a react component that contains one or more React Router 4 Route components to be rendered.
For example:
// App.jsimportReactfrom'react';import{Route}from'react-router';importHomefrom'../containers/Home';importArticlefrom'../containers/Article';constApp=()=><div><h1>This is my App!</h1><Routepath="/"component={Home}exact/><Routepath="/article"component={Article}exact/></div>;// index.js// ...importsimportAppfrom'./App';// ... koa app setupapp.use(reactrouter({
App,// Other callbacks}));
preRender
Callback function called before rendering App.
This callback can either be a normal function which returns a valid component or it can return a Promise which then resolves and returns a valid component.
The function accepts the following parameters:
component - The StaticRouter wrapped around the App.
This callback could be used to wrap the component with any other higher-order component before it gets rendered
onError
Callback function called when an error occurs whilst route matching or rendering.
The function accepts the following parameters:
Callback function called before sending a response to the client.
This function must be supplied, and must return an object that contains the following property:
Container
This should be a React component that wraps around the rendered route.
Typically this will be the template for the page, however this is not mandatory.
As such this component is rendered using renderToStaticMarkup.
The component must accept the children prop and insert it when rendered.
For example:
As well as the Container property this callback can optionally return:
containerRenderer
Optional function for handling the rendering of a container component.
This function has one argument which is view. This argument is the currently rendered view of the app.
This function may be used if some custom props need to be injected into the container component, such as an initial Redux state.
This function should be used instead of the Container property when returning from onRender.
For example you may want to render the container as follows:
<htmllang="en"><head><script>//State config</script></head><body><p>hello container</p><div><!-- View html in here --></div></body></html>
id
Optional id for React to use as the base of the app. Default: app
In order for React to re-hydrate the DOM on the client it needs to know where it should attach itself. In a previous version of koa-react-router this was not possible. This property allows you to add a custom root id to the DOM. For example, if you set this to root, the output would look something like.
<htmllang="en"><head><script>//State config</script></head><body><divid="root"><!-- View html in here --></div></body></html>
Router Context
React Router 4 added support for a static router context this context can be used in your application, to pass values from your router to other middleware and drive behaviour for routes. koa-react-router makes this context available on the koa ctx in the following location:
ctx.state.routerContext;
One example of using this context is setting a status in the route context so a later middleware can set that as this response code.
The common use case of status is already taken care of. So if one of your routes sets a status prop whilst rendering that will be set as the response status See Not found in the FAQ section for an example.
Use the routerContext for whatever you want in your app, some common recipes will be added to this repo at a later date.
FAQ
This release includes some deprecated props. As React Router has come with some major changes so has koa-react-router.
No more routes prop ?
The routes prop has gone in favour of the App config prop. Where you would have passed in your static routes before you can now pass in your App component that contains the React Router routes. For example:
// App.jsimportReactfrom'react';import{Route}from'react-router';importHomefrom'../containers/Home';importArticlefrom'../containers/Article';constApp=()=><div><h1>This is my App!</h1><Routepath="/"component={Home}exact/><Routepath="/article"component={Article}exact/></div>;
React Router 4 gives you the flexibility to define your routes wherever you want in your app, and so does koa-react-router.
What do I do with routes that are not found ?
The previous version of koa-react-router supported a onNotFound callback. This has been deprecated in favour of defining a status prop on the React Router static context and using a Switch component in your app. For example, our App component may be written as:
importReactfrom'react';import{Route,Switch}from'react-router';importHomefrom'../containers/Home';importArticlefrom'../containers/Article';constNotFound=({ status })=><Routerender={({ staticContext })=>{if(staticContext)staticContext.status=status;return<div>This route has not been found Soz!</div>;}}/>;constApp=()=><div><h1>This is my App!</h1><Switch><Routepath="/"component={Home}exact/><Routepath="/article"component={Article}exact/><NotFoundstatus={404}/></Switch></div>;
If not other routes are matched the NotFound component will be rendered, and koa-react-router will set the response code status.
请发表评论