Git Product home page Git Product logo

react-view's Introduction

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.

License

MIT

react-view's People

Contributors

dead-horse avatar tejasmanohar avatar lukebennett avatar kingscooty avatar yandongxu avatar

Watchers

rosa maria palacios juncosa avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.