createStaticRouter
createStaticRouter
用于在您想要利用 数据路由器 在服务器上进行渲染时(例如,Node 或其他 Javascript 运行时)。有关更完整的概述,请参阅 服务器端渲染 指南。
import {
createStaticHandler,
createStaticRouter,
StaticRouterProvider,
} from "react-router-dom/server";
import Root, {
loader as rootLoader,
ErrorBoundary as RootBoundary,
} from "./root";
const routes = [
{
path: "/",
loader: rootLoader,
Component: Root,
ErrorBoundary: RootBoundary,
},
];
export async function renderHtml(req) {
let { query, dataRoutes } = createStaticHandler(routes);
let fetchRequest = createFetchRequest(req);
let context = await query(fetchRequest);
// If we got a redirect response, short circuit and let our Express server
// handle that directly
if (context instanceof Response) {
throw context;
}
let router = createStaticRouter(dataRoutes, context);
return ReactDOMServer.renderToString(
<React.StrictMode>
<StaticRouterProvider
router={router}
context={context}
/>
</React.StrictMode>
);
}
declare function createStaticRouter(
routes: RouteObject[],
context: StaticHandlerContext,
opts: {
future?: {
v7_partialHydration?: boolean;
};
}
): Router;
opts.future
一组可选的 未来标志,用于为该静态路由器启用。我们建议您尽早选择新发布的未来标志,以便更轻松地迁移到 v7。
const router = createBrowserRouter(routes, {
future: {
// Opt-into partial hydration
v7_partialHydration: true,
},
});
目前提供以下未来标志
标志 | 描述 |
---|---|
v7_partialHydration |
支持服务器渲染应用程序的部分水合 |
另请参阅