- Notifications
You must be signed in to change notification settings - Fork29
koajs/react-view
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A Koa view engine which renders React components on server.
$ npm install koa-react-view
varreact=require('koa-react-view');varpath=require('path');varkoa=require('koa');varapp=koa();varviewpath=path.join(__dirname,'views');varassetspath=path.join(__dirname,'public');react(app,{views:viewpath});app.use(function*(){this.render(home,{foo:'bar'});});
This module no longer includes theBabel runtime, as that prevented developersfrom using the runtime on the server outside of the scope of this module. Additionally,Babel recommends that the polyfill is only included by the parent app to avoid theseconflicts. If you'd like to use JSX, ES6, or other features that require transpiling,you can include Babel in your project directly. Seeexample.
option | values | default |
---|---|---|
doctype | any string that can be used asa doctype, this will be prepended to your document | "<!DOCTYPE html>" |
beautify | true : beautify markup before outputting (note, this can affect rendering due to additional whitespace) | false |
views | the root directory of view files | path.join(__dirname, 'views') |
extname | the default view file's extname | jsx |
writeResp | true : writes the body response automatically | true |
cache | true : cache all the view files | process.env.NODE_ENV === 'production' |
internals | true : include React internals in output | false |
React provides two ways to render components server-side:
ReactDOMServer.renderToStaticMarkup strips out all the React internals, reducing the size of the output. Best for static sites.
ReactDOMServer.renderToString maintains React internals, allowing for client-side React to process the rendered markup very speedily. Best for an initial server-side rendering of a client-side application.
By default, theReactDOMServer.renderToStaticMarkup
method will be used. It is possible to useReactDOMServer.renderToString
instead (and maintain the React internals) by setting theinternals
option totrue
, or by setting the third parameter ofthis.render
totrue
on a case-by-case basis.
koa-react-view
supportctx.state in koa.
MIT
About
A Koa view engine which renders React components on server