React HTTP status code allows you to set status codes from react components
npm install react-http-status-codeReact HTTP status code allows you to set status codes from react components
javascript
import React from "react";
import { StatusCode } from "react-http-status-code";// render your component
const MyComponent = () => (
Sorry, page was not found
);
`Server usage
`javascript
import React from "react";
import { renderToString } from "react-dom/server";
import { StatusCode } from "react-http-status-code";// 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);
};
``