本页还没有译文
本页 32 段还没译成日本語,下面显示的是英文原文。createMemoryRouter
还没翻译译自英文(reactrouter.com)本页 32 段都还没译
createMemoryRouter
Data
Summary
Create a new DataRouter that manages the application path using an
in-memory History
stack. Useful for non-browser environments without a DOM API.
Data Routers should not be held in React state. You should create your router
once outside of the React tree and pass it to <RouterProvider>.
You can use patchRoutesOnNavigation to add additional routes programmatically.
Signature
function createMemoryRouter(
routes: RouteObject[],
opts?: MemoryRouterOpts,
): DataRouterParams
routes
Application routes
opts.basename
Basename path for the application.
opts.dataStrategy
Override the default data strategy of running loaders in parallel - see the docs for more information.
let router = createBrowserRouter(routes, {
async dataStrategy({
matches,
request,
runClientMiddleware,
}) {
const matchesToLoad = matches.filter((m) =>
m.shouldCallHandler(),
);
const results: Record<string, DataStrategyResult> = {};
await runClientMiddleware(() =>
Promise.all(
matchesToLoad.map(async (match) => {
results[match.route.id] = await match.resolve();
}),
),
);
return results;
},
});opts.future
Future flags to enable for the router.
opts.getContext
A function that returns an RouterContextProvider instance
which is provided as the context argument to client actions,
loaders and middleware.
This function is called to generate a fresh context instance on each
navigation or fetcher call.
opts.hydrationData
Hydration data to initialize the router with if you have already performed data loading on the server.
opts.initialEntries
Initial entries in the in-memory history stack
opts.initialIndex
Index of initialEntries the application should initialize to
opts.instrumentations
Array of instrumentation objects allowing you to instrument the router and
individual routes prior to router initialization (and on any subsequently
added routes via route.lazy or patchRoutesOnNavigation). This is
mostly useful for observability such as wrapping navigations, fetches,
as well as route loaders/actions/middlewares with logging and/or performance
tracing. See the docs for more information.
let router = createBrowserRouter(routes, {
instrumentations: [logging]
});
let logging = {
router({ instrument }) {
instrument({
navigate: (impl, info) => logExecution(`navigate ${info.to}`, impl),
fetch: (impl, info) => logExecution(`fetch ${info.to}`, impl)
});
},
route({ instrument, id }) {
instrument({
middleware: (impl, info) => logExecution(
`middleware ${info.request.url} (route ${id})`,
impl
),
loader: (impl, info) => logExecution(
`loader ${info.request.url} (route ${id})`,
impl
),
action: (impl, info) => logExecution(
`action ${info.request.url} (route ${id})`,
impl
),
})
}
};
async function logExecution(label: string, impl: () => Promise<void>) {
let start = performance.now();
console.log(`start ${label}`);
await impl();
let duration = Math.round(performance.now() - start);
console.log(`end ${label} (${duration}ms)`);
}opts.patchRoutesOnNavigation
Lazily define portions of the route tree on navigations.
Returns
An initialized DataRouter to pass to <RouterProvider>