| 1 |
|
| 2 | import { Action, InitialEntry, Location, To } from "./router/history.js";
|
| 3 | import { DataStrategyFunction, IndexRouteObject, LazyRouteFunction, NonIndexRouteObject, Params, PatchRoutesOnNavigationFunction, RouteMatch, RouteObject, UIMatch } from "./router/utils.js";
|
| 4 | import { FutureConfig, HydrationState, RelativeRoutingType, Router as Router$1, RouterInit } from "./router/router.js";
|
| 5 | import { Navigator } from "./context.js";
|
| 6 | import { ClientInstrumentation } from "./router/instrumentation.js";
|
| 7 | import * as React$1 from "react";
|
| 8 |
|
| 9 | //#region lib/components.d.ts
|
| 10 | declare const hydrationRouteProperties: (keyof RouteObject)[];
|
| 11 | /**
|
| 12 | * @category Data Routers
|
| 13 | */
|
| 14 | interface MemoryRouterOpts {
|
| 15 | /**
|
| 16 | * Basename path for the application.
|
| 17 | */
|
| 18 | basename?: string;
|
| 19 | /**
|
| 20 | * A function that returns an {@link RouterContextProvider} instance
|
| 21 | * which is provided as the `context` argument to client [`action`](../../start/data/route-object#action)s,
|
| 22 | * [`loader`](../../start/data/route-object#loader)s and [middleware](../../how-to/middleware).
|
| 23 | * This function is called to generate a fresh `context` instance on each
|
| 24 | * navigation or fetcher call.
|
| 25 | */
|
| 26 | getContext?: RouterInit["getContext"];
|
| 27 | /**
|
| 28 | * Future flags to enable for the router.
|
| 29 | */
|
| 30 | future?: Partial<FutureConfig>;
|
| 31 | /**
|
| 32 | * Hydration data to initialize the router with if you have already performed
|
| 33 | * data loading on the server.
|
| 34 | */
|
| 35 | hydrationData?: HydrationState;
|
| 36 | /**
|
| 37 | * Initial entries in the in-memory history stack
|
| 38 | */
|
| 39 | initialEntries?: InitialEntry[];
|
| 40 | /**
|
| 41 | * Index of `initialEntries` the application should initialize to
|
| 42 | */
|
| 43 | initialIndex?: number;
|
| 44 | /**
|
| 45 | * Array of instrumentation objects allowing you to instrument the router and
|
| 46 | * individual routes prior to router initialization (and on any subsequently
|
| 47 | * added routes via `route.lazy` or `patchRoutesOnNavigation`). This is
|
| 48 | * mostly useful for observability such as wrapping navigations, fetches,
|
| 49 | * as well as route loaders/actions/middlewares with logging and/or performance
|
| 50 | * tracing. See the [docs](../../how-to/instrumentation) for more information.
|
| 51 | *
|
| 52 | * ```tsx
|
| 53 | * let router = createBrowserRouter(routes, {
|
| 54 | * instrumentations: [logging]
|
| 55 | * });
|
| 56 | *
|
| 57 | *
|
| 58 | * let logging = {
|
| 59 | * router({ instrument }) {
|
| 60 | * instrument({
|
| 61 | * navigate: (impl, info) => logExecution(`navigate ${info.to}`, impl),
|
| 62 | * fetch: (impl, info) => logExecution(`fetch ${info.to}`, impl)
|
| 63 | * });
|
| 64 | * },
|
| 65 | * route({ instrument, id }) {
|
| 66 | * instrument({
|
| 67 | * middleware: (impl, info) => logExecution(
|
| 68 | * `middleware ${info.request.url} (route ${id})`,
|
| 69 | * impl
|
| 70 | * ),
|
| 71 | * loader: (impl, info) => logExecution(
|
| 72 | * `loader ${info.request.url} (route ${id})`,
|
| 73 | * impl
|
| 74 | * ),
|
| 75 | * action: (impl, info) => logExecution(
|
| 76 | * `action ${info.request.url} (route ${id})`,
|
| 77 | * impl
|
| 78 | * ),
|
| 79 | * })
|
| 80 | * }
|
| 81 | * };
|
| 82 | *
|
| 83 | * async function logExecution(label: string, impl: () => Promise<void>) {
|
| 84 | * let start = performance.now();
|
| 85 | * console.log(`start ${label}`);
|
| 86 | * await impl();
|
| 87 | * let duration = Math.round(performance.now() - start);
|
| 88 | * console.log(`end ${label} (${duration}ms)`);
|
| 89 | * }
|
| 90 | * ```
|
| 91 | */
|
| 92 | instrumentations?: ClientInstrumentation[];
|
| 93 | /**
|
| 94 | * Override the default data strategy of running loaders in parallel -
|
| 95 | * see the [docs](../../how-to/data-strategy) for more information.
|
| 96 | *
|
| 97 | * ```tsx
|
| 98 | * let router = createBrowserRouter(routes, {
|
| 99 | * async dataStrategy({
|
| 100 | * matches,
|
| 101 | * request,
|
| 102 | * runClientMiddleware,
|
| 103 | * }) {
|
| 104 | * const matchesToLoad = matches.filter((m) =>
|
| 105 | * m.shouldCallHandler(),
|
| 106 | * );
|
| 107 | *
|
| 108 | * const results: Record<string, DataStrategyResult> = {};
|
| 109 | * await runClientMiddleware(() =>
|
| 110 | * Promise.all(
|
| 111 | * matchesToLoad.map(async (match) => {
|
| 112 | * results[match.route.id] = await match.resolve();
|
| 113 | * }),
|
| 114 | * ),
|
| 115 | * );
|
| 116 | * return results;
|
| 117 | * },
|
| 118 | * });
|
| 119 | * ```
|
| 120 | */
|
| 121 | dataStrategy?: DataStrategyFunction;
|
| 122 | /**
|
| 123 | * Lazily define portions of the route tree on navigations.
|
| 124 | */
|
| 125 | patchRoutesOnNavigation?: PatchRoutesOnNavigationFunction;
|
| 126 | }
|
| 127 | /**
|
| 128 | * Create a new {@link DataRouter} that manages the application path using an
|
| 129 | * in-memory [`History`](https://developer.mozilla.org/en-US/docs/Web/API/History)
|
| 130 | * stack. Useful for non-browser environments without a DOM API.
|
| 131 | *
|
| 132 | * Data Routers should not be held in React state. You should create your router
|
| 133 | * once outside of the React tree and pass it to {@link RouterProvider | `<RouterProvider>`}.
|
| 134 | * You can use `patchRoutesOnNavigation` to add additional routes programmatically.
|
| 135 | *
|
| 136 | * @public
|
| 137 | * @category Data Routers
|
| 138 | * @mode data
|
| 139 | * @param routes Application routes
|
| 140 | * @param opts Options
|
| 141 | * @param {MemoryRouterOpts.basename} opts.basename n/a
|
| 142 | * @param {MemoryRouterOpts.dataStrategy} opts.dataStrategy n/a
|
| 143 | * @param {MemoryRouterOpts.future} opts.future n/a
|
| 144 | * @param {MemoryRouterOpts.getContext} opts.getContext n/a
|
| 145 | * @param {MemoryRouterOpts.hydrationData} opts.hydrationData n/a
|
| 146 | * @param {MemoryRouterOpts.initialEntries} opts.initialEntries n/a
|
| 147 | * @param {MemoryRouterOpts.initialIndex} opts.initialIndex n/a
|
| 148 | * @param {MemoryRouterOpts.instrumentations} opts.instrumentations n/a
|
| 149 | * @param {MemoryRouterOpts.patchRoutesOnNavigation} opts.patchRoutesOnNavigation n/a
|
| 150 | * @returns An initialized {@link DataRouter} to pass to {@link RouterProvider | `<RouterProvider>`}
|
| 151 | */
|
| 152 | declare function createMemoryRouter(routes: RouteObject[], opts?: MemoryRouterOpts): Router$1;
|
| 153 | /**
|
| 154 | * Function signature for client side error handling for loader/actions errors
|
| 155 | * and rendering errors via `componentDidCatch`
|
| 156 | */
|
| 157 | interface ClientOnErrorFunction {
|
| 158 | (error: unknown, info: {
|
| 159 | location: Location;
|
| 160 | params: Params;
|
| 161 | pattern: string;
|
| 162 | errorInfo?: React$1.ErrorInfo;
|
| 163 | }): void;
|
| 164 | }
|
| 165 | /**
|
| 166 | * @category Types
|
| 167 | */
|
| 168 | interface RouterProviderProps {
|
| 169 | /**
|
| 170 | * The {@link DataRouter} instance to use for navigation and data fetching. The
|
| 171 | * router prop should be a single router instance created outside of the React
|
| 172 | * tree. Avoid creating new routers during React renders/re-renders.
|
| 173 | */
|
| 174 | router: Router$1;
|
| 175 | /**
|
| 176 | * The [`ReactDOM.flushSync`](https://react.dev/reference/react-dom/flushSync)
|
| 177 | * implementation to use for flushing updates.
|
| 178 | *
|
| 179 | * You usually don't have to worry about this:
|
| 180 | * - The `RouterProvider` exported from `react-router/dom` handles this internally for you
|
| 181 | * - If you are rendering in a non-DOM environment, you can import
|
| 182 | * `RouterProvider` from `react-router` and ignore this prop
|
| 183 | */
|
| 184 | flushSync?: <R>(fn: () => R) => R;
|
| 185 | /**
|
| 186 | * An error handler function that will be called for any middleware, loader, action,
|
| 187 | * or render errors that are encountered in your application. This is useful for
|
| 188 | * logging or reporting errors instead of in the {@link ErrorBoundary} because it's not
|
| 189 | * subject to re-rendering and will only run one time per error.
|
| 190 | *
|
| 191 | * The `errorInfo` parameter is passed along from
|
| 192 | * [`componentDidCatch`](https://react.dev/reference/react/Component#componentdidcatch)
|
| 193 | * and is only present for render errors.
|
| 194 | *
|
| 195 | * ```tsx
|
| 196 | * <RouterProvider onError={(error, info) => {
|
| 197 | * let { location, params, pattern, errorInfo } = info;
|
| 198 | * console.error(error, location, errorInfo);
|
| 199 | * reportToErrorService(error, location, errorInfo);
|
| 200 | * }} />
|
| 201 | * ```
|
| 202 | */
|
| 203 | onError?: ClientOnErrorFunction;
|
| 204 | /**
|
| 205 | * Control whether router state updates are internally wrapped in
|
| 206 | * [`React.startTransition`](https://react.dev/reference/react/startTransition).
|
| 207 | *
|
| 208 | * - When left `undefined`, all state updates are wrapped in
|
| 209 | * `React.startTransition`
|
| 210 | * - This can lead to buggy behaviors if you are wrapping your own
|
| 211 | * navigations/fetchers in `startTransition`.
|
| 212 | * - When set to `true`, {@link Link} and {@link Form} navigations will be wrapped
|
| 213 | * in `React.startTransition` and router state changes will be wrapped in
|
| 214 | * `React.startTransition` and also sent through
|
| 215 | * [`useOptimistic`](https://react.dev/reference/react/useOptimistic) to
|
| 216 | * surface mid-navigation router state changes to the UI.
|
| 217 | * - When set to `false`, the router will not leverage `React.startTransition` or
|
| 218 | * `React.useOptimistic` on any navigations or state changes.
|
| 219 | *
|
| 220 | * For more information, please see the [docs](../../explanation/react-transitions).
|
| 221 | */
|
| 222 | useTransitions?: boolean;
|
| 223 | }
|
| 224 | /**
|
| 225 | * Render the UI for the given {@link DataRouter}. This component should
|
| 226 | * typically be at the top of an app's element tree. The router prop should
|
| 227 | * be a single router instance created outside of the React tree. Avoid
|
| 228 | * creating new routers during React renders/re-renders.
|
| 229 | *
|
| 230 | * ```tsx
|
| 231 | * import { createBrowserRouter } from "react-router";
|
| 232 | * import { RouterProvider } from "react-router/dom";
|
| 233 | * import { createRoot } from "react-dom/client";
|
| 234 | *
|
| 235 | * const router = createBrowserRouter(routes);
|
| 236 | * createRoot(document.getElementById("root")).render(
|
| 237 | * <RouterProvider router={router} />
|
| 238 | * );
|
| 239 | * ```
|
| 240 | *
|
| 241 | * <docs-info>Please note that this component is exported both from
|
| 242 | * `react-router` and `react-router/dom` with the only difference being that the
|
| 243 | * latter automatically wires up `react-dom`'s [`flushSync`](https://react.dev/reference/react-dom/flushSync)
|
| 244 | * implementation. You _almost always_ want to use the version from
|
| 245 | * `react-router/dom` unless you're running in a non-DOM environment.</docs-info>
|
| 246 | *
|
| 247 | *
|
| 248 | * @public
|
| 249 | * @category Data Routers
|
| 250 | * @mode data
|
| 251 | * @param props Props
|
| 252 | * @param {RouterProviderProps.flushSync} props.flushSync n/a
|
| 253 | * @param {RouterProviderProps.onError} props.onError n/a
|
| 254 | * @param {RouterProviderProps.router} props.router n/a
|
| 255 | * @param {RouterProviderProps.useTransitions} props.useTransitions n/a
|
| 256 | * @returns React element for the rendered router
|
| 257 | */
|
| 258 | declare function RouterProvider({
|
| 259 | router,
|
| 260 | flushSync: reactDomFlushSyncImpl,
|
| 261 | onError,
|
| 262 | useTransitions
|
| 263 | }: RouterProviderProps): React$1.ReactElement;
|
| 264 | /**
|
| 265 | * @category Types
|
| 266 | */
|
| 267 | interface MemoryRouterProps {
|
| 268 | /**
|
| 269 | * Application basename
|
| 270 | */
|
| 271 | basename?: string;
|
| 272 | /**
|
| 273 | * Nested {@link Route} elements describing the route tree
|
| 274 | */
|
| 275 | children?: React$1.ReactNode;
|
| 276 | /**
|
| 277 | * Initial entries in the in-memory history stack
|
| 278 | */
|
| 279 | initialEntries?: InitialEntry[];
|
| 280 | /**
|
| 281 | * Index of `initialEntries` the application should initialize to
|
| 282 | */
|
| 283 | initialIndex?: number;
|
| 284 | /**
|
| 285 | * Control whether router state updates are internally wrapped in
|
| 286 | * [`React.startTransition`](https://react.dev/reference/react/startTransition).
|
| 287 | *
|
| 288 | * - When left `undefined`, all router state updates are wrapped in
|
| 289 | * `React.startTransition`
|
| 290 | * - When set to `true`, {@link Link} and {@link Form} navigations will be wrapped
|
| 291 | * in `React.startTransition` and all router state updates are wrapped in
|
| 292 | * `React.startTransition`
|
| 293 | * - When set to `false`, the router will not leverage `React.startTransition`
|
| 294 | * on any navigations or state changes.
|
| 295 | *
|
| 296 | * For more information, please see the [docs](../../explanation/react-transitions).
|
| 297 | */
|
| 298 | useTransitions?: boolean;
|
| 299 | }
|
| 300 | /**
|
| 301 | * A declarative {@link Router | `<Router>`} that stores all entries in memory.
|
| 302 | *
|
| 303 | * @public
|
| 304 | * @category Declarative Routers
|
| 305 | * @mode declarative
|
| 306 | * @param props Props
|
| 307 | * @param {MemoryRouterProps.basename} props.basename n/a
|
| 308 | * @param {MemoryRouterProps.children} props.children n/a
|
| 309 | * @param {MemoryRouterProps.initialEntries} props.initialEntries n/a
|
| 310 | * @param {MemoryRouterProps.initialIndex} props.initialIndex n/a
|
| 311 | * @param {MemoryRouterProps.useTransitions} props.useTransitions n/a
|
| 312 | * @returns A declarative in-memory {@link Router | `<Router>`} for client-side
|
| 313 | * routing.
|
| 314 | */
|
| 315 | declare function MemoryRouter({
|
| 316 | basename,
|
| 317 | children,
|
| 318 | initialEntries,
|
| 319 | initialIndex,
|
| 320 | useTransitions
|
| 321 | }: MemoryRouterProps): React$1.ReactElement;
|
| 322 | /**
|
| 323 | * @category Types
|
| 324 | */
|
| 325 | interface NavigateProps {
|
| 326 | /**
|
| 327 | * The path to navigate to. This can be a string or a {@link Path} object
|
| 328 | */
|
| 329 | to: To;
|
| 330 | /**
|
| 331 | * Whether to replace the current entry in the [`History`](https://developer.mozilla.org/en-US/docs/Web/API/History)
|
| 332 | * stack
|
| 333 | */
|
| 334 | replace?: boolean;
|
| 335 | /**
|
| 336 | * State to pass to the new {@link Location} to store in [`history.state`](https://developer.mozilla.org/en-US/docs/Web/API/History/state).
|
| 337 | */
|
| 338 | state?: any;
|
| 339 | /**
|
| 340 | * How to interpret relative routing in the `to` prop.
|
| 341 | * See {@link RelativeRoutingType}.
|
| 342 | */
|
| 343 | relative?: RelativeRoutingType;
|
| 344 | }
|
| 345 | /**
|
| 346 | * A component-based version of {@link useNavigate} to use in a
|
| 347 | * [`React.Component` class](https://react.dev/reference/react/Component) where
|
| 348 | * hooks cannot be used.
|
| 349 | *
|
| 350 | * It's recommended to avoid using this component in favor of {@link useNavigate}.
|
| 351 | *
|
| 352 | * @example
|
| 353 | * <Navigate to="/tasks" />
|
| 354 | *
|
| 355 | * @public
|
| 356 | * @category Components
|
| 357 | * @param props Props
|
| 358 | * @param {NavigateProps.relative} props.relative n/a
|
| 359 | * @param {NavigateProps.replace} props.replace n/a
|
| 360 | * @param {NavigateProps.state} props.state n/a
|
| 361 | * @param {NavigateProps.to} props.to n/a
|
| 362 | * @returns {void}
|
| 363 | *
|
| 364 | */
|
| 365 | declare function Navigate({
|
| 366 | to,
|
| 367 | replace,
|
| 368 | state,
|
| 369 | relative
|
| 370 | }: NavigateProps): null;
|
| 371 | /**
|
| 372 | * @category Types
|
| 373 | */
|
| 374 | interface OutletProps {
|
| 375 | /**
|
| 376 | * Provides a context value to the element tree below the outlet. Use when
|
| 377 | * the parent route needs to provide values to child routes.
|
| 378 | *
|
| 379 | * ```tsx
|
| 380 | * <Outlet context={myContextValue} />
|
| 381 | * ```
|
| 382 | *
|
| 383 | * Access the context with {@link useOutletContext}.
|
| 384 | */
|
| 385 | context?: unknown;
|
| 386 | }
|
| 387 | /**
|
| 388 | * Renders the matching child route of a parent route or nothing if no child
|
| 389 | * route matches.
|
| 390 | *
|
| 391 | * @example
|
| 392 | * import { Outlet } from "react-router";
|
| 393 | *
|
| 394 | * export default function SomeParent() {
|
| 395 | * return (
|
| 396 | * <div>
|
| 397 | * <h1>Parent Content</h1>
|
| 398 | * <Outlet />
|
| 399 | * </div>
|
| 400 | * );
|
| 401 | * }
|
| 402 | *
|
| 403 | * @public
|
| 404 | * @category Components
|
| 405 | * @param props Props
|
| 406 | * @param {OutletProps.context} props.context n/a
|
| 407 | * @returns React element for the rendered outlet or `null` if no child route matches.
|
| 408 | */
|
| 409 | declare function Outlet(props: OutletProps): React$1.ReactElement | null;
|
| 410 | /**
|
| 411 | * @category Types
|
| 412 | */
|
| 413 | interface PathRouteProps {
|
| 414 | /**
|
| 415 | * Whether the path should be case-sensitive. Defaults to `false`.
|
| 416 | */
|
| 417 | caseSensitive?: NonIndexRouteObject["caseSensitive"];
|
| 418 | /**
|
| 419 | * The path pattern to match. If unspecified or empty, then this becomes a
|
| 420 | * layout route.
|
| 421 | */
|
| 422 | path?: NonIndexRouteObject["path"];
|
| 423 | /**
|
| 424 | * The unique identifier for this route (for use with {@link DataRouter}s)
|
| 425 | */
|
| 426 | id?: NonIndexRouteObject["id"];
|
| 427 | /**
|
| 428 | * A function that returns a promise that resolves to the route object.
|
| 429 | * Used for code-splitting routes.
|
| 430 | * See [`lazy`](../../start/data/route-object#lazy).
|
| 431 | */
|
| 432 | lazy?: LazyRouteFunction<NonIndexRouteObject>;
|
| 433 | /**
|
| 434 | * The route middleware.
|
| 435 | * See [`middleware`](../../start/data/route-object#middleware).
|
| 436 | */
|
| 437 | middleware?: NonIndexRouteObject["middleware"];
|
| 438 | /**
|
| 439 | * The route loader.
|
| 440 | * See [`loader`](../../start/data/route-object#loader).
|
| 441 | */
|
| 442 | loader?: NonIndexRouteObject["loader"];
|
| 443 | /**
|
| 444 | * The route action.
|
| 445 | * See [`action`](../../start/data/route-object#action).
|
| 446 | */
|
| 447 | action?: NonIndexRouteObject["action"];
|
| 448 | /**
|
| 449 | * The route shouldRevalidate function.
|
| 450 | * See [`shouldRevalidate`](../../start/data/route-object#shouldRevalidate).
|
| 451 | */
|
| 452 | shouldRevalidate?: NonIndexRouteObject["shouldRevalidate"];
|
| 453 | /**
|
| 454 | * The route handle.
|
| 455 | */
|
| 456 | handle?: NonIndexRouteObject["handle"];
|
| 457 | /**
|
| 458 | * Whether this is an index route.
|
| 459 | */
|
| 460 | index?: false;
|
| 461 | /**
|
| 462 | * Child Route components
|
| 463 | */
|
| 464 | children?: React$1.ReactNode;
|
| 465 | /**
|
| 466 | * The React element to render when this Route matches.
|
| 467 | * Mutually exclusive with `Component`.
|
| 468 | */
|
| 469 | element?: React$1.ReactNode | null;
|
| 470 | /**
|
| 471 | * The React element to render while this router is loading data.
|
| 472 | * Mutually exclusive with `HydrateFallback`.
|
| 473 | */
|
| 474 | hydrateFallbackElement?: React$1.ReactNode | null;
|
| 475 | /**
|
| 476 | * The React element to render at this route if an error occurs.
|
| 477 | * Mutually exclusive with `ErrorBoundary`.
|
| 478 | */
|
| 479 | errorElement?: React$1.ReactNode | null;
|
| 480 | /**
|
| 481 | * The React Component to render when this route matches.
|
| 482 | * Mutually exclusive with `element`.
|
| 483 | */
|
| 484 | Component?: React$1.ComponentType | null;
|
| 485 | /**
|
| 486 | * The React Component to render while this router is loading data.
|
| 487 | * Mutually exclusive with `hydrateFallbackElement`.
|
| 488 | */
|
| 489 | HydrateFallback?: React$1.ComponentType | null;
|
| 490 | /**
|
| 491 | * The React Component to render at this route if an error occurs.
|
| 492 | * Mutually exclusive with `errorElement`.
|
| 493 | */
|
| 494 | ErrorBoundary?: React$1.ComponentType | null;
|
| 495 | }
|
| 496 | /**
|
| 497 | * @category Types
|
| 498 | */
|
| 499 | interface LayoutRouteProps extends PathRouteProps {}
|
| 500 | /**
|
| 501 | * @category Types
|
| 502 | */
|
| 503 | interface IndexRouteProps {
|
| 504 | /**
|
| 505 | * Whether the path should be case-sensitive. Defaults to `false`.
|
| 506 | */
|
| 507 | caseSensitive?: IndexRouteObject["caseSensitive"];
|
| 508 | /**
|
| 509 | * The path pattern to match. If unspecified or empty, then this becomes a
|
| 510 | * layout route.
|
| 511 | */
|
| 512 | path?: IndexRouteObject["path"];
|
| 513 | /**
|
| 514 | * The unique identifier for this route (for use with {@link DataRouter}s)
|
| 515 | */
|
| 516 | id?: IndexRouteObject["id"];
|
| 517 | /**
|
| 518 | * A function that returns a promise that resolves to the route object.
|
| 519 | * Used for code-splitting routes.
|
| 520 | * See [`lazy`](../../start/data/route-object#lazy).
|
| 521 | */
|
| 522 | lazy?: LazyRouteFunction<IndexRouteObject>;
|
| 523 | /**
|
| 524 | * The route middleware.
|
| 525 | * See [`middleware`](../../start/data/route-object#middleware).
|
| 526 | */
|
| 527 | middleware?: IndexRouteObject["middleware"];
|
| 528 | /**
|
| 529 | * The route loader.
|
| 530 | * See [`loader`](../../start/data/route-object#loader).
|
| 531 | */
|
| 532 | loader?: IndexRouteObject["loader"];
|
| 533 | /**
|
| 534 | * The route action.
|
| 535 | * See [`action`](../../start/data/route-object#action).
|
| 536 | */
|
| 537 | action?: IndexRouteObject["action"];
|
| 538 | /**
|
| 539 | * The route shouldRevalidate function.
|
| 540 | * See [`shouldRevalidate`](../../start/data/route-object#shouldRevalidate).
|
| 541 | */
|
| 542 | shouldRevalidate?: IndexRouteObject["shouldRevalidate"];
|
| 543 | /**
|
| 544 | * The route handle.
|
| 545 | */
|
| 546 | handle?: IndexRouteObject["handle"];
|
| 547 | /**
|
| 548 | * Whether this is an index route.
|
| 549 | */
|
| 550 | index: true;
|
| 551 | /**
|
| 552 | * Child Route components
|
| 553 | */
|
| 554 | children?: undefined;
|
| 555 | /**
|
| 556 | * The React element to render when this Route matches.
|
| 557 | * Mutually exclusive with `Component`.
|
| 558 | */
|
| 559 | element?: React$1.ReactNode | null;
|
| 560 | /**
|
| 561 | * The React element to render while this router is loading data.
|
| 562 | * Mutually exclusive with `HydrateFallback`.
|
| 563 | */
|
| 564 | hydrateFallbackElement?: React$1.ReactNode | null;
|
| 565 | /**
|
| 566 | * The React element to render at this route if an error occurs.
|
| 567 | * Mutually exclusive with `ErrorBoundary`.
|
| 568 | */
|
| 569 | errorElement?: React$1.ReactNode | null;
|
| 570 | /**
|
| 571 | * The React Component to render when this route matches.
|
| 572 | * Mutually exclusive with `element`.
|
| 573 | */
|
| 574 | Component?: React$1.ComponentType | null;
|
| 575 | /**
|
| 576 | * The React Component to render while this router is loading data.
|
| 577 | * Mutually exclusive with `hydrateFallbackElement`.
|
| 578 | */
|
| 579 | HydrateFallback?: React$1.ComponentType | null;
|
| 580 | /**
|
| 581 | * The React Component to render at this route if an error occurs.
|
| 582 | * Mutually exclusive with `errorElement`.
|
| 583 | */
|
| 584 | ErrorBoundary?: React$1.ComponentType | null;
|
| 585 | }
|
| 586 | /**
|
| 587 | * @category Types
|
| 588 | */
|
| 589 | type RouteProps = PathRouteProps | LayoutRouteProps | IndexRouteProps;
|
| 590 | /**
|
| 591 | * Configures an element to render when a pattern matches the current location.
|
| 592 | * It must be rendered within a {@link Routes} element. Note that these routes
|
| 593 | * do not participate in data loading, actions, code splitting, or any other
|
| 594 | * route module features.
|
| 595 | *
|
| 596 | * @example
|
| 597 | * // Usually used in a declarative router
|
| 598 | * function App() {
|
| 599 | * return (
|
| 600 | * <BrowserRouter>
|
| 601 | * <Routes>
|
| 602 | * <Route index element={<StepOne />} />
|
| 603 | * <Route path="step-2" element={<StepTwo />} />
|
| 604 | * <Route path="step-3" element={<StepThree />} />
|
| 605 | * </Routes>
|
| 606 | * </BrowserRouter>
|
| 607 | * );
|
| 608 | * }
|
| 609 | *
|
| 610 | * // But can be used with a data router as well if you prefer the JSX notation
|
| 611 | * const routes = createRoutesFromElements(
|
| 612 | * <>
|
| 613 | * <Route index loader={step1Loader} Component={StepOne} />
|
| 614 | * <Route path="step-2" loader={step2Loader} Component={StepTwo} />
|
| 615 | * <Route path="step-3" loader={step3Loader} Component={StepThree} />
|
| 616 | * </>
|
| 617 | * );
|
| 618 | *
|
| 619 | * const router = createBrowserRouter(routes);
|
| 620 | *
|
| 621 | * function App() {
|
| 622 | * return <RouterProvider router={router} />;
|
| 623 | * }
|
| 624 | *
|
| 625 | * @public
|
| 626 | * @category Components
|
| 627 | * @param props Props
|
| 628 | * @param {PathRouteProps.action} props.action n/a
|
| 629 | * @param {PathRouteProps.caseSensitive} props.caseSensitive n/a
|
| 630 | * @param {PathRouteProps.Component} props.Component n/a
|
| 631 | * @param {PathRouteProps.children} props.children n/a
|
| 632 | * @param {PathRouteProps.element} props.element n/a
|
| 633 | * @param {PathRouteProps.ErrorBoundary} props.ErrorBoundary n/a
|
| 634 | * @param {PathRouteProps.errorElement} props.errorElement n/a
|
| 635 | * @param {PathRouteProps.handle} props.handle n/a
|
| 636 | * @param {PathRouteProps.HydrateFallback} props.HydrateFallback n/a
|
| 637 | * @param {PathRouteProps.hydrateFallbackElement} props.hydrateFallbackElement n/a
|
| 638 | * @param {PathRouteProps.id} props.id n/a
|
| 639 | * @param {PathRouteProps.index} props.index n/a
|
| 640 | * @param {PathRouteProps.lazy} props.lazy n/a
|
| 641 | * @param {PathRouteProps.loader} props.loader n/a
|
| 642 | * @param {PathRouteProps.path} props.path n/a
|
| 643 | * @param {PathRouteProps.shouldRevalidate} props.shouldRevalidate n/a
|
| 644 | * @returns {void}
|
| 645 | */
|
| 646 | declare function Route(props: RouteProps): React$1.ReactElement | null;
|
| 647 | /**
|
| 648 | * @category Types
|
| 649 | */
|
| 650 | interface RouterProps {
|
| 651 | /**
|
| 652 | * The base path for the application. This is prepended to all locations
|
| 653 | */
|
| 654 | basename?: string;
|
| 655 | /**
|
| 656 | * Nested {@link Route} elements describing the route tree
|
| 657 | */
|
| 658 | children?: React$1.ReactNode;
|
| 659 | /**
|
| 660 | * The location to match against. Defaults to the current location.
|
| 661 | * This can be a string or a {@link Location} object.
|
| 662 | */
|
| 663 | location: Partial<Location> | string;
|
| 664 | /**
|
| 665 | * The type of navigation that triggered this `location` change.
|
| 666 | * Defaults to {@link NavigationType.Pop}.
|
| 667 | */
|
| 668 | navigationType?: Action;
|
| 669 | /**
|
| 670 | * The navigator to use for navigation. This is usually a history object
|
| 671 | * or a custom navigator that implements the {@link Navigator} interface.
|
| 672 | */
|
| 673 | navigator: Navigator;
|
| 674 | /**
|
| 675 | * Whether this router is static or not (used for SSR). If `true`, the router
|
| 676 | * will not be reactive to location changes.
|
| 677 | */
|
| 678 | static?: boolean;
|
| 679 | /**
|
| 680 | * Control whether router state updates are internally wrapped in
|
| 681 | * [`React.startTransition`](https://react.dev/reference/react/startTransition).
|
| 682 | *
|
| 683 | * - When left `undefined`, all router state updates are wrapped in
|
| 684 | * `React.startTransition`
|
| 685 | * - When set to `true`, {@link Link} and {@link Form} navigations will be wrapped
|
| 686 | * in `React.startTransition` and all router state updates are wrapped in
|
| 687 | * `React.startTransition`
|
| 688 | * - When set to `false`, the router will not leverage `React.startTransition`
|
| 689 | * on any navigations or state changes.
|
| 690 | *
|
| 691 | * For more information, please see the [docs](../../explanation/react-transitions).
|
| 692 | */
|
| 693 | useTransitions?: boolean;
|
| 694 | }
|
| 695 | /**
|
| 696 | * Provides location context for the rest of the app.
|
| 697 | *
|
| 698 | * Note: You usually won't render a `<Router>` directly. Instead, you'll render a
|
| 699 | * router that is more specific to your environment such as a {@link BrowserRouter}
|
| 700 | * in web browsers or a {@link ServerRouter} for server rendering.
|
| 701 | *
|
| 702 | * @public
|
| 703 | * @category Declarative Routers
|
| 704 | * @mode declarative
|
| 705 | * @param props Props
|
| 706 | * @param {RouterProps.basename} props.basename n/a
|
| 707 | * @param {RouterProps.children} props.children n/a
|
| 708 | * @param {RouterProps.location} props.location n/a
|
| 709 | * @param {RouterProps.navigationType} props.navigationType n/a
|
| 710 | * @param {RouterProps.navigator} props.navigator n/a
|
| 711 | * @param {RouterProps.static} props.static n/a
|
| 712 | * @param {RouterProps.useTransitions} props.useTransitions n/a
|
| 713 | * @returns React element for the rendered router or `null` if the location does
|
| 714 | * not match the {@link props.basename}
|
| 715 | */
|
| 716 | declare function Router({
|
| 717 | basename: basenameProp,
|
| 718 | children,
|
| 719 | location: locationProp,
|
| 720 | navigationType,
|
| 721 | navigator,
|
| 722 | static: staticProp,
|
| 723 | useTransitions
|
| 724 | }: RouterProps): React$1.ReactElement | null;
|
| 725 | /**
|
| 726 | * @category Types
|
| 727 | */
|
| 728 | interface RoutesProps {
|
| 729 | /**
|
| 730 | * Nested {@link Route} elements
|
| 731 | */
|
| 732 | children?: React$1.ReactNode;
|
| 733 | /**
|
| 734 | * The {@link Location} to match against. Defaults to the current location.
|
| 735 | */
|
| 736 | location?: Partial<Location> | string;
|
| 737 | }
|
| 738 | /**
|
| 739 | * Renders a branch of {@link Route | `<Route>`s} that best matches the current
|
| 740 | * location. Note that these routes do not participate in [data loading](../../start/framework/route-module#loader),
|
| 741 | * [`action`](../../start/framework/route-module#action), code splitting, or
|
| 742 | * any other [route module](../../start/framework/route-module) features.
|
| 743 | *
|
| 744 | * @example
|
| 745 | * import { Route, Routes } from "react-router";
|
| 746 | *
|
| 747 | * <Routes>
|
| 748 | * <Route index element={<StepOne />} />
|
| 749 | * <Route path="step-2" element={<StepTwo />} />
|
| 750 | * <Route path="step-3" element={<StepThree />} />
|
| 751 | * </Routes>
|
| 752 | *
|
| 753 | * @public
|
| 754 | * @category Components
|
| 755 | * @param props Props
|
| 756 | * @param {RoutesProps.children} props.children n/a
|
| 757 | * @param {RoutesProps.location} props.location n/a
|
| 758 | * @returns React element for the rendered routes or `null` if no route matches
|
| 759 | */
|
| 760 | declare function Routes({
|
| 761 | children,
|
| 762 | location
|
| 763 | }: RoutesProps): React$1.ReactElement | null;
|
| 764 | interface AwaitResolveRenderFunction<Resolve = any> {
|
| 765 | (data: Awaited<Resolve>): React$1.ReactNode;
|
| 766 | }
|
| 767 | /**
|
| 768 | * @category Types
|
| 769 | */
|
| 770 | interface AwaitProps<Resolve> {
|
| 771 | /**
|
| 772 | * When using a function, the resolved value is provided as the parameter.
|
| 773 | *
|
| 774 | * ```tsx [2]
|
| 775 | * <Await resolve={reviewsPromise}>
|
| 776 | * {(resolvedReviews) => <Reviews items={resolvedReviews} />}
|
| 777 | * </Await>
|
| 778 | * ```
|
| 779 | *
|
| 780 | * When using React elements, {@link useAsyncValue} will provide the
|
| 781 | * resolved value:
|
| 782 | *
|
| 783 | * ```tsx [2]
|
| 784 | * <Await resolve={reviewsPromise}>
|
| 785 | * <Reviews />
|
| 786 | * </Await>
|
| 787 | *
|
| 788 | * function Reviews() {
|
| 789 | * const resolvedReviews = useAsyncValue();
|
| 790 | * return <div>...</div>;
|
| 791 | * }
|
| 792 | * ```
|
| 793 | */
|
| 794 | children: React$1.ReactNode | AwaitResolveRenderFunction<Resolve>;
|
| 795 | /**
|
| 796 | * The error element renders instead of the `children` when the [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)
|
| 797 | * rejects.
|
| 798 | *
|
| 799 | * ```tsx
|
| 800 | * <Await
|
| 801 | * errorElement={<div>Oops</div>}
|
| 802 | * resolve={reviewsPromise}
|
| 803 | * >
|
| 804 | * <Reviews />
|
| 805 | * </Await>
|
| 806 | * ```
|
| 807 | *
|
| 808 | * To provide a more contextual error, you can use the {@link useAsyncError} in a
|
| 809 | * child component
|
| 810 | *
|
| 811 | * ```tsx
|
| 812 | * <Await
|
| 813 | * errorElement={<ReviewsError />}
|
| 814 | * resolve={reviewsPromise}
|
| 815 | * >
|
| 816 | * <Reviews />
|
| 817 | * </Await>
|
| 818 | *
|
| 819 | * function ReviewsError() {
|
| 820 | * const error = useAsyncError();
|
| 821 | * return <div>Error loading reviews: {error.message}</div>;
|
| 822 | * }
|
| 823 | * ```
|
| 824 | *
|
| 825 | * If you do not provide an `errorElement`, the rejected value will bubble up
|
| 826 | * to the nearest route-level [`ErrorBoundary`](../../start/framework/route-module#errorboundary)
|
| 827 | * and be accessible via the {@link useRouteError} hook.
|
| 828 | */
|
| 829 | errorElement?: React$1.ReactNode;
|
| 830 | /**
|
| 831 | * Takes a [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)
|
| 832 | * returned from a [`loader`](../../start/framework/route-module#loader) to be
|
| 833 | * resolved and rendered.
|
| 834 | *
|
| 835 | * ```tsx
|
| 836 | * import { Await, useLoaderData } from "react-router";
|
| 837 | *
|
| 838 | * export async function loader() {
|
| 839 | * let reviews = getReviews(); // not awaited
|
| 840 | * let book = await getBook();
|
| 841 | * return {
|
| 842 | * book,
|
| 843 | * reviews, // this is a promise
|
| 844 | * };
|
| 845 | * }
|
| 846 | *
|
| 847 | * export default function Book() {
|
| 848 | * const {
|
| 849 | * book,
|
| 850 | * reviews, // this is the same promise
|
| 851 | * } = useLoaderData();
|
| 852 | *
|
| 853 | * return (
|
| 854 | * <div>
|
| 855 | * <h1>{book.title}</h1>
|
| 856 | * <p>{book.description}</p>
|
| 857 | * <React.Suspense fallback={<ReviewsSkeleton />}>
|
| 858 | * <Await
|
| 859 | * // and is the promise we pass to Await
|
| 860 | * resolve={reviews}
|
| 861 | * >
|
| 862 | * <Reviews />
|
| 863 | * </Await>
|
| 864 | * </React.Suspense>
|
| 865 | * </div>
|
| 866 | * );
|
| 867 | * }
|
| 868 | * ```
|
| 869 | */
|
| 870 | resolve: Resolve;
|
| 871 | }
|
| 872 | /**
|
| 873 | * Used to render promise values with automatic error handling.
|
| 874 | *
|
| 875 | * **Note:** `<Await>` expects to be rendered inside a [`<React.Suspense>`](https://react.dev/reference/react/Suspense)
|
| 876 | *
|
| 877 | * @example
|
| 878 | * import { Await, useLoaderData } from "react-router";
|
| 879 | *
|
| 880 | * export async function loader() {
|
| 881 | * // not awaited
|
| 882 | * const reviews = getReviews();
|
| 883 | * // awaited (blocks the transition)
|
| 884 | * const book = await fetch("/api/book").then((res) => res.json());
|
| 885 | * return { book, reviews };
|
| 886 | * }
|
| 887 | *
|
| 888 | * function Book() {
|
| 889 | * const { book, reviews } = useLoaderData();
|
| 890 | * return (
|
| 891 | * <div>
|
| 892 | * <h1>{book.title}</h1>
|
| 893 | * <p>{book.description}</p>
|
| 894 | * <React.Suspense fallback={<ReviewsSkeleton />}>
|
| 895 | * <Await
|
| 896 | * resolve={reviews}
|
| 897 | * errorElement={
|
| 898 | * <div>Could not load reviews 😬</div>
|
| 899 | * }
|
| 900 | * children={(resolvedReviews) => (
|
| 901 | * <Reviews items={resolvedReviews} />
|
| 902 | * )}
|
| 903 | * />
|
| 904 | * </React.Suspense>
|
| 905 | * </div>
|
| 906 | * );
|
| 907 | * }
|
| 908 | *
|
| 909 | * @public
|
| 910 | * @category Components
|
| 911 | * @mode framework
|
| 912 | * @mode data
|
| 913 | * @param props Props
|
| 914 | * @param {AwaitProps.children} props.children n/a
|
| 915 | * @param {AwaitProps.errorElement} props.errorElement n/a
|
| 916 | * @param {AwaitProps.resolve} props.resolve n/a
|
| 917 | * @returns React element for the rendered awaited value
|
| 918 | */
|
| 919 | declare function Await<Resolve>({
|
| 920 | children,
|
| 921 | errorElement,
|
| 922 | resolve
|
| 923 | }: AwaitProps<Resolve>): React$1.JSX.Element;
|
| 924 | /**
|
| 925 | * Creates a route config from a React "children" object, which is usually
|
| 926 | * either a `<Route>` element or an array of them. Used internally by
|
| 927 | * `<Routes>` to create a route config from its children.
|
| 928 | *
|
| 929 | * @category Utils
|
| 930 | * @mode data
|
| 931 | * @param children The React children to convert into a route config
|
| 932 | * @param parentPath The path of the parent route, used to generate unique IDs.
|
| 933 | * @returns An array of {@link RouteObject}s that can be used with a {@link DataRouter}
|
| 934 | */
|
| 935 | declare function createRoutesFromChildren(children: React$1.ReactNode, parentPath?: number[]): RouteObject[];
|
| 936 | /**
|
| 937 | * Create route objects from JSX elements instead of arrays of objects.
|
| 938 | *
|
| 939 | * @example
|
| 940 | * const routes = createRoutesFromElements(
|
| 941 | * <>
|
| 942 | * <Route index loader={step1Loader} Component={StepOne} />
|
| 943 | * <Route path="step-2" loader={step2Loader} Component={StepTwo} />
|
| 944 | * <Route path="step-3" loader={step3Loader} Component={StepThree} />
|
| 945 | * </>
|
| 946 | * );
|
| 947 | *
|
| 948 | * const router = createBrowserRouter(routes);
|
| 949 | *
|
| 950 | * function App() {
|
| 951 | * return <RouterProvider router={router} />;
|
| 952 | * }
|
| 953 | *
|
| 954 | * @name createRoutesFromElements
|
| 955 | * @public
|
| 956 | * @category Utils
|
| 957 | * @mode data
|
| 958 | * @param children The React children to convert into a route config
|
| 959 | * @param parentPath The path of the parent route, used to generate unique IDs.
|
| 960 | * This is used for internal recursion and is not intended to be used by the
|
| 961 | * application developer.
|
| 962 | * @returns An array of {@link RouteObject}s that can be used with a {@link DataRouter}
|
| 963 | */
|
| 964 | declare const createRoutesFromElements: typeof createRoutesFromChildren;
|
| 965 | /**
|
| 966 | * Renders the result of {@link matchRoutes} into a React element.
|
| 967 | *
|
| 968 | * @public
|
| 969 | * @category Utils
|
| 970 | * @param matches The array of {@link RouteMatch | route matches} to render
|
| 971 | * @returns A React element that renders the matched routes or `null` if no matches
|
| 972 | */
|
| 973 | declare function renderMatches(matches: RouteMatch[] | null): React$1.ReactElement | null;
|
| 974 | declare function useRouteComponentProps(): {
|
| 975 | params: Readonly<Params<string>>;
|
| 976 | loaderData: any;
|
| 977 | actionData: any;
|
| 978 | matches: UIMatch<unknown, unknown>[];
|
| 979 | };
|
| 980 | type RouteComponentProps = ReturnType<typeof useRouteComponentProps>;
|
| 981 | type RouteComponentType = React$1.ComponentType<RouteComponentProps>;
|
| 982 | declare function WithComponentProps({
|
| 983 | children
|
| 984 | }: {
|
| 985 | children: React$1.ReactElement;
|
| 986 | }): React$1.ReactElement<unknown, string | React$1.JSXElementConstructor<any>>;
|
| 987 | declare function withComponentProps(Component: RouteComponentType): () => React$1.ReactElement<{
|
| 988 | params: Readonly<Params<string>>;
|
| 989 | loaderData: any;
|
| 990 | actionData: any;
|
| 991 | matches: UIMatch<unknown, unknown>[];
|
| 992 | }, string | React$1.JSXElementConstructor<any>>;
|
| 993 | declare function useHydrateFallbackProps(): {
|
| 994 | params: Readonly<Params<string>>;
|
| 995 | loaderData: any;
|
| 996 | actionData: any;
|
| 997 | };
|
| 998 | type HydrateFallbackProps = ReturnType<typeof useHydrateFallbackProps>;
|
| 999 | type HydrateFallbackType = React$1.ComponentType<HydrateFallbackProps>;
|
| 1000 | declare function WithHydrateFallbackProps({
|
| 1001 | children
|
| 1002 | }: {
|
| 1003 | children: React$1.ReactElement;
|
| 1004 | }): React$1.ReactElement<unknown, string | React$1.JSXElementConstructor<any>>;
|
| 1005 | declare function withHydrateFallbackProps(HydrateFallback: HydrateFallbackType): () => React$1.ReactElement<{
|
| 1006 | params: Readonly<Params<string>>;
|
| 1007 | loaderData: any;
|
| 1008 | actionData: any;
|
| 1009 | }, string | React$1.JSXElementConstructor<any>>;
|
| 1010 | declare function useErrorBoundaryProps(): {
|
| 1011 | params: Readonly<Params<string>>;
|
| 1012 | loaderData: any;
|
| 1013 | actionData: any;
|
| 1014 | error: unknown;
|
| 1015 | };
|
| 1016 | type ErrorBoundaryProps = ReturnType<typeof useErrorBoundaryProps>;
|
| 1017 | type ErrorBoundaryType = React$1.ComponentType<ErrorBoundaryProps>;
|
| 1018 | declare function WithErrorBoundaryProps({
|
| 1019 | children
|
| 1020 | }: {
|
| 1021 | children: React$1.ReactElement;
|
| 1022 | }): React$1.ReactElement<unknown, string | React$1.JSXElementConstructor<any>>;
|
| 1023 | declare function withErrorBoundaryProps(ErrorBoundary: ErrorBoundaryType): () => React$1.ReactElement<{
|
| 1024 | params: Readonly<Params<string>>;
|
| 1025 | loaderData: any;
|
| 1026 | actionData: any;
|
| 1027 | error: unknown;
|
| 1028 | }, string | React$1.JSXElementConstructor<any>>;
|
| 1029 | //#endregion
|
| 1030 | export { Await, AwaitProps, ClientOnErrorFunction, IndexRouteProps, LayoutRouteProps, MemoryRouter, MemoryRouterOpts, MemoryRouterProps, Navigate, NavigateProps, Outlet, OutletProps, PathRouteProps, Route, RouteProps, Router, RouterProps, RouterProvider, RouterProviderProps, Routes, RoutesProps, WithComponentProps, WithErrorBoundaryProps, WithHydrateFallbackProps, createMemoryRouter, createRoutesFromChildren, createRoutesFromElements, hydrationRouteProperties, renderMatches, withComponentProps, withErrorBoundaryProps, withHydrateFallbackProps }; |
| \ | No newline at end of file |