Set the HTTP status code from inside React components
npm install react-http-statusSet the HTTP status code from inside react components
``sh`
npm install --save konnektid/konnektid-react-status
`js
import React from "react";
import StatusCode from "konnektid-react-status";
// render your component Sorry, page was not found
const MyComponent = () => (
);
`
`js
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);
};
``