本页还没有译文
本页 23 段还没译成한국어,下面显示的是英文原文。RouterProvider
还没翻译译自英文(reactrouter.com)本页 23 段都还没译
RouterProvider
Data
Summary
Render the UI for the given DataRouter. This component should
typically be at the top of an app's element tree. The router prop should
be a single router instance created outside of the React tree. Avoid
creating new routers during React renders/re-renders.
import { createBrowserRouter } from "react-router";
import { RouterProvider } from "react-router/dom";
import { createRoot } from "react-dom/client";
const router = createBrowserRouter(routes);
createRoot(document.getElementById("root")).render(
<RouterProvider router={router} />
);Info
Please note that this component is exported both from
react-router and react-router/dom with the only difference being that the
latter automatically wires up react-dom's flushSync
implementation. You almost always want to use the version from
react-router/dom unless you're running in a non-DOM environment.
Signature
function RouterProvider({
router,
flushSync: reactDomFlushSyncImpl,
onError,
useTransitions,
}: RouterProviderProps): React.ReactElementProps
flushSync
The ReactDOM.flushSync
implementation to use for flushing updates.
You usually don't have to worry about this:
- The
RouterProviderexported fromreact-router/domhandles this internally for you - If you are rendering in a non-DOM environment, you can import
RouterProviderfromreact-routerand ignore this prop
onError
An error handler function that will be called for any middleware, loader, action,
or render errors that are encountered in your application. This is useful for
logging or reporting errors instead of in the ErrorBoundary because it's not
subject to re-rendering and will only run one time per error.
The errorInfo parameter is passed along from
componentDidCatch
and is only present for render errors.
<RouterProvider onError={(error, info) => {
let { location, params, pattern, errorInfo } = info;
console.error(error, location, errorInfo);
reportToErrorService(error, location, errorInfo);
}} />router
The DataRouter instance to use for navigation and data fetching. The
router prop should be a single router instance created outside of the React
tree. Avoid creating new routers during React renders/re-renders.
useTransitions
Control whether router state updates are internally wrapped in
React.startTransition.
- When left
undefined, all state updates are wrapped inReact.startTransition- This can lead to buggy behaviors if you are wrapping your own
navigations/fetchers in
startTransition.
- This can lead to buggy behaviors if you are wrapping your own
navigations/fetchers in
- When set to
true,LinkandFormnavigations will be wrapped inReact.startTransitionand router state changes will be wrapped inReact.startTransitionand also sent throughuseOptimisticto surface mid-navigation router state changes to the UI. - When set to
false, the router will not leverageReact.startTransitionorReact.useOptimisticon any navigations or state changes.
For more information, please see the docs.