react-pjax
Simple middleware of PJAX implementation with ReactJS
Semantics
This module provides a solution for how to use PJAX with ReactJS, when a request with custom PJAX header is received, only state will be returned for partial update without server-side rendered markup. If it is not a PJAX request, markup and state will be returned together.
Install
$ npm install react-pjax
API
var reactPjax = ;
reactPjax(options)
Create a new middleware with the options as follow.
Options
The module accepts these properties in the options object.
reqHeader (optional)
The header name to identify whether it is a PJAX request. Default is 'X-REACT-PJAX'.
reactStateTag (optional)
The property name of state in the returned object inside res.send() and res.render(). Default is 'reactState'.
reactMarkupTag (optional)
The property name of markup in the returned object inside res.send() and res.render(). Default is 'reactState'.
Usage
There are currently two methods, res.pjaxJson and res.pjaxRender.
res.pjaxJson(reactElement,reactState,meta)
It returns a JSON.
reactElement
React element. It is defined by var MyReactElement = React.createFactory(require('MyReactComponent'));
reactState
React state in JSON format. It is used in server side rendering (SSR). e.g. React.renderToString(MyReactElement(reactState));
meta (optional)
Meta is an object with properties. (e.g. title)
res.pjaxRender(view,reactElement,reactState,meta)
Render your view with passing markup and state, sends the rendered HTML to client side.
view
Name of view.
Example
Application entry point (index.js)
var reactPjax = ;var express = var app = ; app; /** * or using default setting * * app.use(reactPjax()); */
Controller handles the request and response
var MyReactElement = React; app; app;
View (index.dust)
{>"layouts/master" /}{body} {markup|s}{/body}
reactState
is the reactStateTag and markup
is the reactMarkupTag.
License
Copyright
Copyright (C) 2015 Tony Ngan, released under the MIT License.