main
Branches
main (6.4.1)dev
Versions
6.4.1v4/5.xv3.x
StaticRouter

<StaticRouter>

Type declaration
declare function StaticRouter(
  props: StaticRouterProps
): React.ReactElement;

interface StaticRouterProps {
  basename?: string;
  children?: React.ReactNode;
  location?: Path | LocationPieces;
}

<StaticRouter> is used to render a React Router web app in node. Provide the current location via the location prop.

  • <StaticRouter location> defaults to "/"
import * as React from "react";
import * as ReactDOMServer from "react-dom/server";
import { StaticRouter } from "react-router-dom/server";
import http from "http";

function requestHandler(req, res) {
  let html = ReactDOMServer.renderToString(
    <StaticRouter location={req.url}>
      {/* The rest of your app goes here */}
    </StaticRouter>
  );

  res.write(html);
  res.end();
}

http.createServer(requestHandler).listen(3000);
Docs and examples CC 4.0
Edit