This module provides a ReactViews
class to extend or mixin, that exposes the
necessary methods to compile and render React components from an HTTP response.
Inspiration from express-react-views. This package aims to offer a similar experience, but without Express.
Install
npm i tilt-react-views -S
Usage
Views are JSX files automatically transpiled by babel-register
, stored in the
views
directory (as configured view .views()
or options.views
).
The default value is ./views
in the current working directory.
views/index.jsx
var React = ; var HelloMessage = React; moduleexports = HelloMessage;
server.js
const path = ;const ReactViews = ; var views = views: path; ;
Or within the stack of middleware, prior to the router handlers. This adds a
.render()
method to the response object.
const views = ;app; app;
Layouts
You can compose your views to decorate your component with a layout.
views/index.jsx
var React = ;var Layout = ; var HelloMessage = React; moduleexports = HelloMessage;
views/layout.jsx
var React = ; moduleexports = React;
Documentation
new ViewsReact(options)
options
options.views
: Views directory to lookup from when rendering (default: 'view/')options.engine
: View extension to use (default:jsx
)options.doctype
: Doctype to use with HTML markup (default:<!doctype html>
)options.renderToString
: When truthy, force the output to be done withrenderToString
(default:renderToStaticMarkup
)
.render(req, res, next, view, props)
Ends the response with Homepage
component HTML markup
resrender = viewsrender;res;
.views(path)
Set the views directory to seach template from.
.engine(ext)
Get or set the file extension to use when loading views.
.react(filename, options)
Require the transpiled component by Babel and creates a new React element. The resulting HTML is sent back to the client.
Tests
Run with npm test
.
var views = ;views; var app = http; ;