主體
分支
主體 (6.23.1)dev
版本
6.23.1v4/5.xv3.x
StaticRouter

<StaticRouter>

類型宣告
declare function StaticRouter(
  props: StaticRouterProps
): React.ReactElement;

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

<StaticRouter> 用於在 node 中渲染 React Router 網路應用程式。透過 location 道具提供當前位置。

  • <StaticRouter location> 預設為 "/"
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);