Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A Koa view engine which renders React components on server

NotificationsYou must be signed in to change notification settings

koajs/react-view

Repository files navigation

NPM versionbuild statusTest coverageDavid depsnode version

A Koa view engine which renders React components on server.

Installation

$ npm install koa-react-view

Usage

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.

Options

optionvaluesdefault
doctypeany string that can be used asa 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, 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.

ctx.state

koa-react-view supportctx.state in koa.

License

MIT

About

A Koa view engine which renders React components on server

Resources

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp