• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

koajs/react-view: A Koa view engine which renders React components on server

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

koajs/react-view

开源软件地址:

https://github.com/koajs/react-view

开源编程语言:

JavaScript 100.0%

开源软件介绍:

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

option values default
doctype any string that can be used as a 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

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




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap