react-http-status

0.0.8 • Public • Published

konnektid-react-status Build Status

Set the HTTP status code from inside react components

Install

npm install --save konnektid/konnektid-react-status

Client usage

import React from "react";
import StatusCode from "konnektid-react-status";
 
// render your component
const MyComponent = () => (
    <StatusCode code={404}>
        <div>
            <p>Sorry, page was not found</p>
        </div>
    </StatusCode>   
);
 

Server usage

import React from "react";
import { renderToString } from "react-dom/server";
import StatusCode from "konnektid-react-status";
 
// express middleware
const renderPage = (req, res, next) => {
 
    const html = renderToString(
        React.createElement(MyRootComponent, props)
    );
 
    // call this AFTER rendering to string!
    const status = StatusCode.rewind();
 
    // send the status if one was defined
    if (status) res.status(status);
 
    res.send(html);
};

Readme

Keywords

none

Package Sidebar

Install

npm i react-http-status

Weekly Downloads

5

Version

0.0.8

License

MIT

Last publish

Collaborators

  • rewop