🚨 Active Supply Chain Attack:node-ipc Package Compromised.Learn More
Socket
Book a DemoSign in
Socket

react-pjax

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-pjax

PJAX implementation with ReactJS

latest
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

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 = require('react-pjax');

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 = require('react-pjax');
var express = require('express')

var app = express();

app.use(reactPjax({
    reqHeader: 'using-pjax',
    reactStateTag: 'myReactState',
    reactMarkupTag: 'myReactMarkup'
}));

/**
 * or using default setting
 *
 * app.use(reactPjax());
 */

Controller handles the request and response

var MyReactElement = React.createFactory(require('MyReactComponent'));

app.get('/', function(req, res) {
    res.pjaxRender('index', MyReactElement, myReactState, {
        title: 'home'
    });
});

app.get('/json', function(req, res) {
    res.pjaxJson(MyReactElement, myReactState, {
        foo: 'bar'
    });
});

View (index.dust)

{>"layouts/master" /}
{body}
    {markup|s}
    <script type="application/json" id="reactState">{reactState|s}</script>
{/body}

reactState is the reactStateTag and markup is the reactMarkupTag.

License

MIT

Copyright (C) 2015 Tony Ngan, released under the MIT License.

Keywords

reactjs

FAQs

Package last updated on 03 Jul 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts