3.0.0 • Published 8 years ago

koa-react-view v3.0.0

Weekly downloads
10
License
MIT
Repository
github
Last release
8 years ago

koa-react-view

NPM version build status Test coverage David deps node version

A Koa view engine which renders React components on server.

Installation

$ npm install koa-react-view

Usage

var react = require('koa-react-view');
var path = require('path');
var koa = require('koa');

var app = koa();

var viewpath = path.join(__dirname, 'views');
var assetspath = path.join(__dirname, 'public');

react(app, {
  views: viewpath
});

app.use(function* () {
  this.render(home, {foo: 'bar'});
});

This module no longer includes the Babel runtime, as that prevented developers from 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 these conflicts. If you'd like to use JSX, ES6, or other features that require transpiling, you can include Babel in your project directly. See example.

Options

optionvaluesdefault
doctypeany string that can be used as a doctype, this will be prepended to your document"<!DOCTYPE html>"
beautifytrue: beautify markup before outputting (note, this can affect rendering due to additional whitespace)false
viewsthe root directory of view filespath.join(__dirname, 'views')
extnamethe default view file's extnamejsx
writeResptrue: writes the body response automaticallytrue
cachetrue: cache all the view filesprocess.env.NODE_ENV === 'production'
internalstrue: include React internals in outputfalse

renderToString vs renderToStaticMarkup

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, the ReactDOMServer.renderToStaticMarkup method will be used. It is possible to use ReactDOMServer.renderToString instead (and maintain the React internals) by setting the internals option to true, or by setting the third parameter of this.render to true on a case-by-case basis.

ctx.state

koa-react-view support ctx.state in koa.

example

License

MIT

3.0.0

8 years ago

2.0.0

9 years ago

1.1.0

9 years ago

1.0.2

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago