| 1 |
|
| 2 | import { History, To } from "../router/history.js";
|
| 3 | import { DataStrategyFunction, HTMLFormMethod, PatchRoutesOnNavigationFunction, RouteObject } from "../router/utils.js";
|
| 4 | import { BlockerFunction, Fetcher, FutureConfig, GetScrollRestorationKeyFunction, HydrationState, RelativeRoutingType, Router, RouterInit } from "../router/router.js";
|
| 5 | import { NavigateOptions } from "../context.js";
|
| 6 | import { FetcherSubmitOptions, SubmitOptions, SubmitTarget, URLSearchParamsInit } from "./dom.js";
|
| 7 | import { DiscoverBehavior, PrefetchBehavior, ScriptsProps } from "./ssr/components.js";
|
| 8 | import { SerializeFrom } from "../types/route-data.js";
|
| 9 | import { ClientInstrumentation } from "../router/instrumentation.js";
|
| 10 | import * as React$1 from "react";
|
| 11 |
|
| 12 |
|
| 13 | |
| 14 | * @category Data Routers
|
| 15 | */
|
| 16 | interface DOMRouterOpts {
|
| 17 | |
| 18 | * Basename path for the application.
|
| 19 | */
|
| 20 | basename?: string;
|
| 21 | |
| 22 | * A function that returns an {@link RouterContextProvider} instance
|
| 23 | * which is provided as the `context` argument to client [`action`](../../start/data/route-object#action)s,
|
| 24 | * [`loader`](../../start/data/route-object#loader)s and [middleware](../../how-to/middleware).
|
| 25 | * This function is called to generate a fresh `context` instance on each
|
| 26 | * navigation or fetcher call.
|
| 27 | *
|
| 28 | * ```tsx
|
| 29 | * import {
|
| 30 | * createContext,
|
| 31 | * RouterContextProvider,
|
| 32 | * } from "react-router";
|
| 33 | *
|
| 34 | * const apiClientContext = createContext<APIClient>();
|
| 35 | *
|
| 36 | * function createBrowserRouter(routes, {
|
| 37 | * getContext() {
|
| 38 | * let context = new RouterContextProvider();
|
| 39 | * context.set(apiClientContext, getApiClient());
|
| 40 | * return context;
|
| 41 | * }
|
| 42 | * })
|
| 43 | * ```
|
| 44 | */
|
| 45 | getContext?: RouterInit["getContext"];
|
| 46 | |
| 47 | * Future flags to enable for the router.
|
| 48 | */
|
| 49 | future?: Partial<FutureConfig>;
|
| 50 | |
| 51 | * When Server-Rendering and opting-out of automatic hydration, the
|
| 52 | * `hydrationData` option allows you to pass in hydration data from your
|
| 53 | * server-render. This will almost always be a subset of data from the
|
| 54 | * {@link StaticHandlerContext} value you get back from the {@link StaticHandler}'s
|
| 55 | * `query` method:
|
| 56 | *
|
| 57 | * ```tsx
|
| 58 | * const router = createBrowserRouter(routes, {
|
| 59 | * hydrationData: {
|
| 60 | * loaderData: {
|
| 61 | *
|
| 62 | * },
|
| 63 | *
|
| 64 | * },
|
| 65 | * });
|
| 66 | * ```
|
| 67 | *
|
| 68 | * **Partial Hydration Data**
|
| 69 | *
|
| 70 | * You will almost always include a complete set of `loaderData` to hydrate a
|
| 71 | * server-rendered app. But in advanced use-cases (such as Framework Mode's
|
| 72 | * [`clientLoader`](../../start/framework/route-module#clientLoader)), you may
|
| 73 | * want to include `loaderData` for only some routes that were loaded/rendered
|
| 74 | * on the server. This allows you to hydrate _some_ of the routes (such as the
|
| 75 | * app layout/shell) while showing a `HydrateFallback` component and running
|
| 76 | * the [`loader`](../../start/data/route-object#loader)s for other routes
|
| 77 | * during hydration.
|
| 78 | *
|
| 79 | * A route [`loader`](../../start/data/route-object#loader) will run during
|
| 80 | * hydration in two scenarios:
|
| 81 | *
|
| 82 | * 1. No hydration data is provided
|
| 83 | * In these cases the `HydrateFallback` component will render on initial
|
| 84 | * hydration
|
| 85 | * 2. The `loader.hydrate` property is set to `true`
|
| 86 | * This allows you to run the [`loader`](../../start/data/route-object#loader)
|
| 87 | * even if you did not render a fallback on initial hydration (i.e., to
|
| 88 | * prime a cache with hydration data)
|
| 89 | *
|
| 90 | * ```tsx
|
| 91 | * const router = createBrowserRouter(
|
| 92 | * [
|
| 93 | * {
|
| 94 | * id: "root",
|
| 95 | * loader: rootLoader,
|
| 96 | * Component: Root,
|
| 97 | * children: [
|
| 98 | * {
|
| 99 | * id: "index",
|
| 100 | * loader: indexLoader,
|
| 101 | * HydrateFallback: IndexSkeleton,
|
| 102 | * Component: Index,
|
| 103 | * },
|
| 104 | * ],
|
| 105 | * },
|
| 106 | * ],
|
| 107 | * {
|
| 108 | * hydrationData: {
|
| 109 | * loaderData: {
|
| 110 | * root: "ROOT DATA",
|
| 111 | *
|
| 112 | * },
|
| 113 | * },
|
| 114 | * }
|
| 115 | * );
|
| 116 | * ```
|
| 117 | */
|
| 118 | hydrationData?: HydrationState;
|
| 119 | |
| 120 | * Array of instrumentation objects allowing you to instrument the router and
|
| 121 | * individual routes prior to router initialization (and on any subsequently
|
| 122 | * added routes via `route.lazy` or `patchRoutesOnNavigation`). This is
|
| 123 | * mostly useful for observability such as wrapping navigations, fetches,
|
| 124 | * as well as route loaders/actions/middlewares with logging and/or performance
|
| 125 | * tracing. See the [docs](../../how-to/instrumentation) for more information.
|
| 126 | *
|
| 127 | * ```tsx
|
| 128 | * let router = createBrowserRouter(routes, {
|
| 129 | * instrumentations: [logging]
|
| 130 | * });
|
| 131 | *
|
| 132 | *
|
| 133 | * let logging = {
|
| 134 | * router({ instrument }) {
|
| 135 | * instrument({
|
| 136 | * navigate: (impl, info) => logExecution(`navigate ${info.to}`, impl),
|
| 137 | * fetch: (impl, info) => logExecution(`fetch ${info.to}`, impl)
|
| 138 | * });
|
| 139 | * },
|
| 140 | * route({ instrument, id }) {
|
| 141 | * instrument({
|
| 142 | * middleware: (impl, info) => logExecution(
|
| 143 | * `middleware ${info.request.url} (route ${id})`,
|
| 144 | * impl
|
| 145 | * ),
|
| 146 | * loader: (impl, info) => logExecution(
|
| 147 | * `loader ${info.request.url} (route ${id})`,
|
| 148 | * impl
|
| 149 | * ),
|
| 150 | * action: (impl, info) => logExecution(
|
| 151 | * `action ${info.request.url} (route ${id})`,
|
| 152 | * impl
|
| 153 | * ),
|
| 154 | * })
|
| 155 | * }
|
| 156 | * };
|
| 157 | *
|
| 158 | * async function logExecution(label: string, impl: () => Promise<void>) {
|
| 159 | * let start = performance.now();
|
| 160 | * console.log(`start ${label}`);
|
| 161 | * await impl();
|
| 162 | * let duration = Math.round(performance.now() - start);
|
| 163 | * console.log(`end ${label} (${duration}ms)`);
|
| 164 | * }
|
| 165 | * ```
|
| 166 | */
|
| 167 | instrumentations?: ClientInstrumentation[];
|
| 168 | |
| 169 | * Override the default data strategy of running loaders in parallel -
|
| 170 | * see the [docs](../../how-to/data-strategy) for more information.
|
| 171 | *
|
| 172 | * ```tsx
|
| 173 | * let router = createBrowserRouter(routes, {
|
| 174 | * async dataStrategy({
|
| 175 | * matches,
|
| 176 | * request,
|
| 177 | * runClientMiddleware,
|
| 178 | * }) {
|
| 179 | * const matchesToLoad = matches.filter((m) =>
|
| 180 | * m.shouldCallHandler(),
|
| 181 | * );
|
| 182 | *
|
| 183 | * const results: Record<string, DataStrategyResult> = {};
|
| 184 | * await runClientMiddleware(() =>
|
| 185 | * Promise.all(
|
| 186 | * matchesToLoad.map(async (match) => {
|
| 187 | * results[match.route.id] = await match.resolve();
|
| 188 | * }),
|
| 189 | * ),
|
| 190 | * );
|
| 191 | * return results;
|
| 192 | * },
|
| 193 | * });
|
| 194 | * ```
|
| 195 | */
|
| 196 | dataStrategy?: DataStrategyFunction;
|
| 197 | |
| 198 | * Lazily define portions of the route tree on navigations.
|
| 199 | * See {@link PatchRoutesOnNavigationFunction}.
|
| 200 | *
|
| 201 | * By default, React Router wants you to provide a full route tree up front via
|
| 202 | * `createBrowserRouter(routes)`. This allows React Router to perform synchronous
|
| 203 | * route matching, execute loaders, and then render route components in the most
|
| 204 | * optimistic manner without introducing waterfalls. The tradeoff is that your
|
| 205 | * initial JS bundle is larger by definition — which may slow down application
|
| 206 | * start-up times as your application grows.
|
| 207 | *
|
| 208 | * To combat this, we introduced [`route.lazy`](../../start/data/route-object#lazy)
|
| 209 | * in [v6.9.0](https:
|
| 210 | * which lets you lazily load the route _implementation_ ([`loader`](../../start/data/route-object#loader),
|
| 211 | * [`Component`](../../start/data/route-object#Component), etc.) while still
|
| 212 | * providing the route _definition_ aspects up front (`path`, `index`, etc.).
|
| 213 | * This is a good middle ground. React Router still knows about your route
|
| 214 | * definitions (the lightweight part) up front and can perform synchronous
|
| 215 | * route matching, but then delay loading any of the route implementation
|
| 216 | * aspects (the heavier part) until the route is actually navigated to.
|
| 217 | *
|
| 218 | * In some cases, even this doesn't go far enough. For huge applications,
|
| 219 | * providing all route definitions up front can be prohibitively expensive.
|
| 220 | * Additionally, it might not even be possible to provide all route definitions
|
| 221 | * up front in certain Micro-Frontend or Module-Federation architectures.
|
| 222 | *
|
| 223 | * This is where `patchRoutesOnNavigation` comes in ([RFC](https:
|
| 224 | * This API is for advanced use-cases where you are unable to provide the full
|
| 225 | * route tree up-front and need a way to lazily "discover" portions of the route
|
| 226 | * tree at runtime. This feature is often referred to as ["Fog of War"](https:
|
| 227 | * because similar to how video games expand the "world" as you move around -
|
| 228 | * the router would be expanding its routing tree as the user navigated around
|
| 229 | * the app - but would only ever end up loading portions of the tree that the
|
| 230 | * user visited.
|
| 231 | *
|
| 232 | * `patchRoutesOnNavigation` will be called anytime React Router is unable to
|
| 233 | * match a `path`. The arguments include the `path`, any partial `matches`,
|
| 234 | * and a `patch` function you can call to patch new routes into the tree at a
|
| 235 | * specific location. This method is executed during the `loading` portion of
|
| 236 | * the navigation for `GET` requests and during the `submitting` portion of
|
| 237 | * the navigation for non-`GET` requests.
|
| 238 | *
|
| 239 | * <details>
|
| 240 | * <summary><b>Example <code>patchRoutesOnNavigation</code> Use Cases</b></summary>
|
| 241 | *
|
| 242 | * **Patching children into an existing route**
|
| 243 | *
|
| 244 | * ```tsx
|
| 245 | * const router = createBrowserRouter(
|
| 246 | * [
|
| 247 | * {
|
| 248 | * id: "root",
|
| 249 | * path: "/",
|
| 250 | * Component: RootComponent,
|
| 251 | * },
|
| 252 | * ],
|
| 253 | * {
|
| 254 | * async patchRoutesOnNavigation({ patch, path }) {
|
| 255 | * if (path === "/a") {
|
| 256 | *
|
| 257 | * let route = await getARoute();
|
| 258 | *
|
| 259 | * patch("root", [route]);
|
| 260 | * }
|
| 261 | * },
|
| 262 | * }
|
| 263 | * );
|
| 264 | * ```
|
| 265 | *
|
| 266 | * In the above example, if the user clicks a link to `/a`, React Router
|
| 267 | * won't match any routes initially and will call `patchRoutesOnNavigation`
|
| 268 | * with a `path = "/a"` and a `matches` array containing the root route
|
| 269 | * match. By calling `patch('root', [route])`, the new route will be added
|
| 270 | * to the route tree as a child of the `root` route and React Router will
|
| 271 | * perform matching on the updated routes. This time it will successfully
|
| 272 | * match the `/a` path and the navigation will complete successfully.
|
| 273 | *
|
| 274 | * **Patching new root-level routes**
|
| 275 | *
|
| 276 | * If you need to patch a new route to the top of the tree (i.e., it doesn't
|
| 277 | * have a parent), you can pass `null` as the `routeId`:
|
| 278 | *
|
| 279 | * ```tsx
|
| 280 | * const router = createBrowserRouter(
|
| 281 | * [
|
| 282 | * {
|
| 283 | * id: "root",
|
| 284 | * path: "/",
|
| 285 | * Component: RootComponent,
|
| 286 | * },
|
| 287 | * ],
|
| 288 | * {
|
| 289 | * async patchRoutesOnNavigation({ patch, path }) {
|
| 290 | * if (path === "/root-sibling") {
|
| 291 | *
|
| 292 | * let route = await getRootSiblingRoute();
|
| 293 | *
|
| 294 | * patch(null, [route]);
|
| 295 | * }
|
| 296 | * },
|
| 297 | * }
|
| 298 | * );
|
| 299 | * ```
|
| 300 | *
|
| 301 | * **Patching subtrees asynchronously**
|
| 302 | *
|
| 303 | * You can also perform asynchronous matching to lazily fetch entire sections
|
| 304 | * of your application:
|
| 305 | *
|
| 306 | * ```tsx
|
| 307 | * let router = createBrowserRouter(
|
| 308 | * [
|
| 309 | * {
|
| 310 | * path: "/",
|
| 311 | * Component: Home,
|
| 312 | * },
|
| 313 | * ],
|
| 314 | * {
|
| 315 | * async patchRoutesOnNavigation({ patch, path }) {
|
| 316 | * if (path.startsWith("/dashboard")) {
|
| 317 | * let children = await import("./dashboard");
|
| 318 | * patch(null, children);
|
| 319 | * }
|
| 320 | * if (path.startsWith("/account")) {
|
| 321 | * let children = await import("./account");
|
| 322 | * patch(null, children);
|
| 323 | * }
|
| 324 | * },
|
| 325 | * }
|
| 326 | * );
|
| 327 | * ```
|
| 328 | *
|
| 329 | * <docs-info>If in-progress execution of `patchRoutesOnNavigation` is
|
| 330 | * interrupted by a later navigation, then any remaining `patch` calls in
|
| 331 | * the interrupted execution will not update the route tree because the
|
| 332 | * operation was cancelled.</docs-info>
|
| 333 | *
|
| 334 | * **Co-locating route discovery with route definition**
|
| 335 | *
|
| 336 | * If you don't wish to perform your own pseudo-matching, you can leverage
|
| 337 | * the partial `matches` array and the [`handle`](../../start/data/route-object#handle)
|
| 338 | * field on a route to keep the children definitions co-located:
|
| 339 | *
|
| 340 | * ```tsx
|
| 341 | * let router = createBrowserRouter(
|
| 342 | * [
|
| 343 | * {
|
| 344 | * path: "/",
|
| 345 | * Component: Home,
|
| 346 | * },
|
| 347 | * {
|
| 348 | * path: "/dashboard",
|
| 349 | * children: [
|
| 350 | * {
|
| 351 | *
|
| 352 | *
|
| 353 | *
|
| 354 | *
|
| 355 | * index: true,
|
| 356 | *
|
| 357 | * },
|
| 358 | * ],
|
| 359 | * handle: {
|
| 360 | * lazyChildren: () => import("./dashboard"),
|
| 361 | * },
|
| 362 | * },
|
| 363 | * {
|
| 364 | * path: "/account",
|
| 365 | * children: [
|
| 366 | * {
|
| 367 | * index: true,
|
| 368 | *
|
| 369 | * },
|
| 370 | * ],
|
| 371 | * handle: {
|
| 372 | * lazyChildren: () => import("./account"),
|
| 373 | * },
|
| 374 | * },
|
| 375 | * ],
|
| 376 | * {
|
| 377 | * async patchRoutesOnNavigation({ matches, patch }) {
|
| 378 | * let leafRoute = matches[matches.length - 1]?.route;
|
| 379 | * if (leafRoute?.handle?.lazyChildren) {
|
| 380 | * let children =
|
| 381 | * await leafRoute.handle.lazyChildren();
|
| 382 | * patch(leafRoute.id, children);
|
| 383 | * }
|
| 384 | * },
|
| 385 | * }
|
| 386 | * );
|
| 387 | * ```
|
| 388 | *
|
| 389 | * **A note on routes with parameters**
|
| 390 | *
|
| 391 | * Because React Router uses ranked routes to find the best match for a
|
| 392 | * given path, there is an interesting ambiguity introduced when only a
|
| 393 | * partial route tree is known at any given point in time. If we match a
|
| 394 | * fully static route such as `path: "/about/contact-us"` then we know we've
|
| 395 | * found the right match since it's composed entirely of static URL segments.
|
| 396 | * Thus, we do not need to bother asking for any other potentially
|
| 397 | * higher-scoring routes.
|
| 398 | *
|
| 399 | * However, routes with parameters (dynamic or splat) can't make this
|
| 400 | * assumption because there might be a not-yet-discovered route that scores
|
| 401 | * higher. Consider a full route tree such as:
|
| 402 | *
|
| 403 | * ```tsx
|
| 404 | *
|
| 405 | * const routes = [
|
| 406 | * {
|
| 407 | * path: "/",
|
| 408 | * Component: Home,
|
| 409 | * },
|
| 410 | * {
|
| 411 | * id: "blog",
|
| 412 | * path: "/blog",
|
| 413 | * Component: BlogLayout,
|
| 414 | * children: [
|
| 415 | * { path: "new", Component: NewPost },
|
| 416 | * { path: ":slug", Component: BlogPost },
|
| 417 | * ],
|
| 418 | * },
|
| 419 | * ];
|
| 420 | * ```
|
| 421 | *
|
| 422 | * And then assume we want to use `patchRoutesOnNavigation` to fill this in
|
| 423 | * as the user navigates around:
|
| 424 | *
|
| 425 | * ```tsx
|
| 426 | *
|
| 427 | * const router = createBrowserRouter(
|
| 428 | * [
|
| 429 | * {
|
| 430 | * path: "/",
|
| 431 | * Component: Home,
|
| 432 | * },
|
| 433 | * ],
|
| 434 | * {
|
| 435 | * async patchRoutesOnNavigation({ patch, path }) {
|
| 436 | * if (path === "/blog/new") {
|
| 437 | * patch("blog", [
|
| 438 | * {
|
| 439 | * path: "new",
|
| 440 | * Component: NewPost,
|
| 441 | * },
|
| 442 | * ]);
|
| 443 | * } else if (path.startsWith("/blog")) {
|
| 444 | * patch("blog", [
|
| 445 | * {
|
| 446 | * path: ":slug",
|
| 447 | * Component: BlogPost,
|
| 448 | * },
|
| 449 | * ]);
|
| 450 | * }
|
| 451 | * },
|
| 452 | * }
|
| 453 | * );
|
| 454 | * ```
|
| 455 | *
|
| 456 | * If the user were to a blog post first (i.e., `/blog/my-post`) we would
|
| 457 | * patch in the `:slug` route. Then, if the user navigated to `/blog/new` to
|
| 458 | * write a new post, we'd match `/blog/:slug` but it wouldn't be the _right_
|
| 459 | * match! We need to call `patchRoutesOnNavigation` just in case there
|
| 460 | * exists a higher-scoring route we've not yet discovered, which in this
|
| 461 | * case there is.
|
| 462 | *
|
| 463 | * So, anytime React Router matches a path that contains at least one param,
|
| 464 | * it will call `patchRoutesOnNavigation` and match routes again just to
|
| 465 | * confirm it has found the best match.
|
| 466 | *
|
| 467 | * If your `patchRoutesOnNavigation` implementation is expensive or making
|
| 468 | * side effect [`fetch`](https:
|
| 469 | * calls to a backend server, you may want to consider tracking previously
|
| 470 | * seen routes to avoid over-fetching in cases where you know the proper
|
| 471 | * route has already been found. This can usually be as simple as
|
| 472 | * maintaining a small cache of prior `path` values for which you've already
|
| 473 | * patched in the right routes:
|
| 474 | *
|
| 475 | * ```tsx
|
| 476 | * let discoveredRoutes = new Set();
|
| 477 | *
|
| 478 | * const router = createBrowserRouter(routes, {
|
| 479 | * async patchRoutesOnNavigation({ patch, path }) {
|
| 480 | * if (discoveredRoutes.has(path)) {
|
| 481 | *
|
| 482 | *
|
| 483 | * return;
|
| 484 | * }
|
| 485 | *
|
| 486 | * discoveredRoutes.add(path);
|
| 487 | *
|
| 488 | *
|
| 489 | * },
|
| 490 | * });
|
| 491 | * ```
|
| 492 | * </details>
|
| 493 | */
|
| 494 | patchRoutesOnNavigation?: PatchRoutesOnNavigationFunction;
|
| 495 | |
| 496 | * [`Window`](https:
|
| 497 | * override. Defaults to the global `window` instance.
|
| 498 | */
|
| 499 | window?: Window;
|
| 500 | }
|
| 501 | |
| 502 | * Create a new {@link DataRouter| data router} that manages the application
|
| 503 | * path via [`history.pushState`](https:
|
| 504 | * and [`history.replaceState`](https:
|
| 505 | *
|
| 506 | * Data Routers should not be held in React state. You should create your router
|
| 507 | * once outside of the React tree and pass it to {@link RouterProvider | `<RouterProvider>`}.
|
| 508 | * You can use `patchRoutesOnNavigation` to add additional routes programmatically.
|
| 509 | *
|
| 510 | * @public
|
| 511 | * @category Data Routers
|
| 512 | * @mode data
|
| 513 | * @param routes Application routes
|
| 514 | * @param opts Options
|
| 515 | * @param {DOMRouterOpts.basename} opts.basename n/a
|
| 516 | * @param {DOMRouterOpts.dataStrategy} opts.dataStrategy n/a
|
| 517 | * @param {DOMRouterOpts.future} opts.future n/a
|
| 518 | * @param {DOMRouterOpts.getContext} opts.getContext n/a
|
| 519 | * @param {DOMRouterOpts.hydrationData} opts.hydrationData n/a
|
| 520 | * @param {DOMRouterOpts.instrumentations} opts.instrumentations n/a
|
| 521 | * @param {DOMRouterOpts.patchRoutesOnNavigation} opts.patchRoutesOnNavigation n/a
|
| 522 | * @param {DOMRouterOpts.window} opts.window n/a
|
| 523 | * @returns An initialized {@link DataRouter| data router} to pass to {@link RouterProvider | `<RouterProvider>`}
|
| 524 | */
|
| 525 | declare function createBrowserRouter(routes: RouteObject[], opts?: DOMRouterOpts): Router;
|
| 526 | |
| 527 | * Create a new {@link DataRouter| data router} that manages the application
|
| 528 | * path via the URL [`hash`](https:
|
| 529 | *
|
| 530 | * Data Routers should not be held in React state. You should create your router
|
| 531 | * once outside of the React tree and pass it to {@link RouterProvider | `<RouterProvider>`}.
|
| 532 | * You can use `patchRoutesOnNavigation` to add additional routes programmatically.
|
| 533 | *
|
| 534 | * @public
|
| 535 | * @category Data Routers
|
| 536 | * @mode data
|
| 537 | * @param routes Application routes
|
| 538 | * @param opts Options
|
| 539 | * @param {DOMRouterOpts.basename} opts.basename n/a
|
| 540 | * @param {DOMRouterOpts.future} opts.future n/a
|
| 541 | * @param {DOMRouterOpts.getContext} opts.getContext n/a
|
| 542 | * @param {DOMRouterOpts.hydrationData} opts.hydrationData n/a
|
| 543 | * @param {DOMRouterOpts.instrumentations} opts.instrumentations n/a
|
| 544 | * @param {DOMRouterOpts.dataStrategy} opts.dataStrategy n/a
|
| 545 | * @param {DOMRouterOpts.patchRoutesOnNavigation} opts.patchRoutesOnNavigation n/a
|
| 546 | * @param {DOMRouterOpts.window} opts.window n/a
|
| 547 | * @returns An initialized {@link DataRouter| data router} to pass to {@link RouterProvider | `<RouterProvider>`}
|
| 548 | */
|
| 549 | declare function createHashRouter(routes: RouteObject[], opts?: DOMRouterOpts): Router;
|
| 550 | |
| 551 | * @category Types
|
| 552 | */
|
| 553 | interface BrowserRouterProps {
|
| 554 | |
| 555 | * Application basename
|
| 556 | */
|
| 557 | basename?: string;
|
| 558 | |
| 559 | * {@link Route | `<Route>`} components describing your route configuration
|
| 560 | */
|
| 561 | children?: React$1.ReactNode;
|
| 562 | |
| 563 | * Control whether router state updates are internally wrapped in
|
| 564 | * [`React.startTransition`](https:
|
| 565 | *
|
| 566 | * - When left `undefined`, all router state updates are wrapped in
|
| 567 | * `React.startTransition`
|
| 568 | * - When set to `true`, {@link Link} and {@link Form} navigations will be wrapped
|
| 569 | * in `React.startTransition` and all router state updates are wrapped in
|
| 570 | * `React.startTransition`
|
| 571 | * - When set to `false`, the router will not leverage `React.startTransition`
|
| 572 | * on any navigations or state changes.
|
| 573 | *
|
| 574 | * For more information, please see the [docs](../../explanation/react-transitions).
|
| 575 | */
|
| 576 | useTransitions?: boolean;
|
| 577 | |
| 578 | * [`Window`](https:
|
| 579 | * override. Defaults to the global `window` instance
|
| 580 | */
|
| 581 | window?: Window;
|
| 582 | }
|
| 583 | |
| 584 | * A declarative {@link Router | `<Router>`} using the browser [`History`](https:
|
| 585 | * API for client-side routing.
|
| 586 | *
|
| 587 | * @public
|
| 588 | * @category Declarative Routers
|
| 589 | * @mode declarative
|
| 590 | * @param props Props
|
| 591 | * @param {BrowserRouterProps.basename} props.basename n/a
|
| 592 | * @param {BrowserRouterProps.children} props.children n/a
|
| 593 | * @param {BrowserRouterProps.useTransitions} props.useTransitions n/a
|
| 594 | * @param {BrowserRouterProps.window} props.window n/a
|
| 595 | * @returns A declarative {@link Router | `<Router>`} using the browser [`History`](https:
|
| 596 | * API for client-side routing.
|
| 597 | */
|
| 598 | declare function BrowserRouter({
|
| 599 | basename,
|
| 600 | children,
|
| 601 | useTransitions,
|
| 602 | window
|
| 603 | }: BrowserRouterProps): React$1.JSX.Element;
|
| 604 | |
| 605 | * @category Types
|
| 606 | */
|
| 607 | interface HashRouterProps {
|
| 608 | |
| 609 | * Application basename
|
| 610 | */
|
| 611 | basename?: string;
|
| 612 | |
| 613 | * {@link Route | `<Route>`} components describing your route configuration
|
| 614 | */
|
| 615 | children?: React$1.ReactNode;
|
| 616 | |
| 617 | * Control whether router state updates are internally wrapped in
|
| 618 | * [`React.startTransition`](https:
|
| 619 | *
|
| 620 | * - When left `undefined`, all router state updates are wrapped in
|
| 621 | * `React.startTransition`
|
| 622 | * - When set to `true`, {@link Link} and {@link Form} navigations will be wrapped
|
| 623 | * in `React.startTransition` and all router state updates are wrapped in
|
| 624 | * `React.startTransition`
|
| 625 | * - When set to `false`, the router will not leverage `React.startTransition`
|
| 626 | * on any navigations or state changes.
|
| 627 | *
|
| 628 | * For more information, please see the [docs](../../explanation/react-transitions).
|
| 629 | */
|
| 630 | useTransitions?: boolean;
|
| 631 | |
| 632 | * [`Window`](https:
|
| 633 | * override. Defaults to the global `window` instance
|
| 634 | */
|
| 635 | window?: Window;
|
| 636 | }
|
| 637 | |
| 638 | * A declarative {@link Router | `<Router>`} that stores the location in the
|
| 639 | * [`hash`](https:
|
| 640 | * of the URL so it is not sent to the server.
|
| 641 | *
|
| 642 | * @public
|
| 643 | * @category Declarative Routers
|
| 644 | * @mode declarative
|
| 645 | * @param props Props
|
| 646 | * @param {HashRouterProps.basename} props.basename n/a
|
| 647 | * @param {HashRouterProps.children} props.children n/a
|
| 648 | * @param {HashRouterProps.useTransitions} props.useTransitions n/a
|
| 649 | * @param {HashRouterProps.window} props.window n/a
|
| 650 | * @returns A declarative {@link Router | `<Router>`} using the URL [`hash`](https:
|
| 651 | * for client-side routing.
|
| 652 | */
|
| 653 | declare function HashRouter({
|
| 654 | basename,
|
| 655 | children,
|
| 656 | useTransitions,
|
| 657 | window
|
| 658 | }: HashRouterProps): React$1.JSX.Element;
|
| 659 | |
| 660 | * @category Types
|
| 661 | */
|
| 662 | interface HistoryRouterProps {
|
| 663 | |
| 664 | * Application basename
|
| 665 | */
|
| 666 | basename?: string;
|
| 667 | |
| 668 | * {@link Route | `<Route>`} components describing your route configuration
|
| 669 | */
|
| 670 | children?: React$1.ReactNode;
|
| 671 | |
| 672 | * A {@link History} implementation for use by the router
|
| 673 | */
|
| 674 | history: History;
|
| 675 | |
| 676 | * Control whether router state updates are internally wrapped in
|
| 677 | * [`React.startTransition`](https:
|
| 678 | *
|
| 679 | * - When left `undefined`, all router state updates are wrapped in
|
| 680 | * `React.startTransition`
|
| 681 | * - When set to `true`, {@link Link} and {@link Form} navigations will be wrapped
|
| 682 | * in `React.startTransition` and all router state updates are wrapped in
|
| 683 | * `React.startTransition`
|
| 684 | * - When set to `false`, the router will not leverage `React.startTransition`
|
| 685 | * on any navigations or state changes.
|
| 686 | *
|
| 687 | * For more information, please see the [docs](../../explanation/react-transitions).
|
| 688 | */
|
| 689 | useTransitions?: boolean;
|
| 690 | }
|
| 691 | |
| 692 | * A declarative {@link Router | `<Router>`} that accepts a pre-instantiated
|
| 693 | * `history` object.
|
| 694 | * It's important to note that using your own `history` object is highly discouraged
|
| 695 | * and may add two versions of the `history` library to your bundles unless you use
|
| 696 | * the same version of the `history` library that React Router uses internally.
|
| 697 | *
|
| 698 | * @name unstable_HistoryRouter
|
| 699 | * @public
|
| 700 | * @category Declarative Routers
|
| 701 | * @mode declarative
|
| 702 | * @param props Props
|
| 703 | * @param {HistoryRouterProps.basename} props.basename n/a
|
| 704 | * @param {HistoryRouterProps.children} props.children n/a
|
| 705 | * @param {HistoryRouterProps.history} props.history n/a
|
| 706 | * @param {HistoryRouterProps.useTransitions} props.useTransitions n/a
|
| 707 | * @returns A declarative {@link Router | `<Router>`} using the provided history
|
| 708 | * implementation for client-side routing.
|
| 709 | */
|
| 710 | declare function HistoryRouter({
|
| 711 | basename,
|
| 712 | children,
|
| 713 | history,
|
| 714 | useTransitions
|
| 715 | }: HistoryRouterProps): React$1.JSX.Element;
|
| 716 | declare namespace HistoryRouter {
|
| 717 | var displayName: string;
|
| 718 | }
|
| 719 | |
| 720 | * @category Types
|
| 721 | */
|
| 722 | interface LinkProps extends Omit<React$1.AnchorHTMLAttributes<HTMLAnchorElement>, "href"> {
|
| 723 | |
| 724 | * Defines the link [lazy route discovery](../../explanation/lazy-route-discovery) behavior.
|
| 725 | *
|
| 726 | * - **render** — default, discover the route when the link renders
|
| 727 | * - **none** — don't eagerly discover, only discover if the link is clicked
|
| 728 | *
|
| 729 | * ```tsx
|
| 730 | * <Link />
|
| 731 | * <Link discover="render" />
|
| 732 | * <Link discover="none" />
|
| 733 | * ```
|
| 734 | */
|
| 735 | discover?: DiscoverBehavior;
|
| 736 | |
| 737 | * Defines the data and module prefetching behavior for the link.
|
| 738 | *
|
| 739 | * ```tsx
|
| 740 | * <Link />
|
| 741 | * <Link prefetch="none" />
|
| 742 | * <Link prefetch="intent" />
|
| 743 | * <Link prefetch="render" />
|
| 744 | * <Link prefetch="viewport" />
|
| 745 | * ```
|
| 746 | *
|
| 747 | * - **none** — default, no prefetching
|
| 748 | * - **intent** — prefetches when the user hovers or focuses the link
|
| 749 | * - **render** — prefetches when the link renders
|
| 750 | * - **viewport** — prefetches when the link is in the viewport, very useful for mobile
|
| 751 | *
|
| 752 | * Prefetching is done with HTML [`<link rel="prefetch">`](https:
|
| 753 | * tags. They are inserted after the link.
|
| 754 | *
|
| 755 | * ```tsx
|
| 756 | * <a href="..." />
|
| 757 | * <a href="..." />
|
| 758 | * <link rel="prefetch" />
|
| 759 | * ```
|
| 760 | *
|
| 761 | * Because of this, if you are using `nav :last-child` you will need to use
|
| 762 | * `nav :last-of-type` so the styles don't conditionally fall off your last link
|
| 763 | * (and any other similar selectors).
|
| 764 | */
|
| 765 | prefetch?: PrefetchBehavior;
|
| 766 | |
| 767 | * Will use document navigation instead of client side routing when the link is
|
| 768 | * clicked: the browser will handle the transition normally (as if it were an
|
| 769 | * [`<a href>`](https:
|
| 770 | *
|
| 771 | * ```tsx
|
| 772 | * <Link to="/logout" reloadDocument />
|
| 773 | * ```
|
| 774 | */
|
| 775 | reloadDocument?: boolean;
|
| 776 | |
| 777 | * Replaces the current entry in the [`History`](https:
|
| 778 | * stack instead of pushing a new one onto it.
|
| 779 | *
|
| 780 | * ```tsx
|
| 781 | * <Link replace />
|
| 782 | * ```
|
| 783 | *
|
| 784 | * ```
|
| 785 | * # with a history stack like this
|
| 786 | * A -> B
|
| 787 | *
|
| 788 | * # normal link click pushes a new entry
|
| 789 | * A -> B -> C
|
| 790 | *
|
| 791 | * # but with `replace`, B is replaced by C
|
| 792 | * A -> C
|
| 793 | * ```
|
| 794 | */
|
| 795 | replace?: boolean;
|
| 796 | |
| 797 | * Adds persistent client side routing state to the next location.
|
| 798 | *
|
| 799 | * ```tsx
|
| 800 | * <Link to="/somewhere/else" state={{ some: "value" }} />
|
| 801 | * ```
|
| 802 | *
|
| 803 | * The location state is accessed from the `location`.
|
| 804 | *
|
| 805 | * ```tsx
|
| 806 | * function SomeComp() {
|
| 807 | * const location = useLocation();
|
| 808 | * location.state;
|
| 809 | * }
|
| 810 | * ```
|
| 811 | *
|
| 812 | * This state is inaccessible on the server as it is implemented on top of
|
| 813 | * [`history.state`](https:
|
| 814 | */
|
| 815 | state?: any;
|
| 816 | |
| 817 | * Prevents the scroll position from being reset to the top of the window when
|
| 818 | * the link is clicked and the app is using {@link ScrollRestoration}. This only
|
| 819 | * prevents new locations resetting scroll to the top, scroll position will be
|
| 820 | * restored for back/forward button navigation.
|
| 821 | *
|
| 822 | * ```tsx
|
| 823 | * <Link to="?tab=one" preventScrollReset />
|
| 824 | * ```
|
| 825 | */
|
| 826 | preventScrollReset?: boolean;
|
| 827 | |
| 828 | * Defines the relative path behavior for the link.
|
| 829 | *
|
| 830 | * ```tsx
|
| 831 | * <Link to=".." />
|
| 832 | * <Link relative="route" />
|
| 833 | * <Link relative="path" />
|
| 834 | * ```
|
| 835 | *
|
| 836 | * Consider a route hierarchy where a parent route pattern is `"blog"` and a child
|
| 837 | * route pattern is `"blog/:slug/edit"`.
|
| 838 | *
|
| 839 | * - **route** — default, resolves the link relative to the route pattern. In the
|
| 840 | * example above, a relative link of `"..."` will remove both `:slug/edit` segments
|
| 841 | * back to `"/blog"`.
|
| 842 | * - **path** — relative to the path so `"..."` will only remove one URL segment up
|
| 843 | * to `"/blog/:slug"`
|
| 844 | *
|
| 845 | * Note that index routes and layout routes do not have paths so they are not
|
| 846 | * included in the relative path calculation.
|
| 847 | */
|
| 848 | relative?: RelativeRoutingType;
|
| 849 | |
| 850 | * Can be a string or a partial {@link Path}:
|
| 851 | *
|
| 852 | * ```tsx
|
| 853 | * <Link to="/some/path" />
|
| 854 | *
|
| 855 | * <Link
|
| 856 | * to={{
|
| 857 | * pathname: "/some/path",
|
| 858 | * search: "?query=string",
|
| 859 | * hash: "#hash",
|
| 860 | * }}
|
| 861 | * />
|
| 862 | * ```
|
| 863 | */
|
| 864 | to: To;
|
| 865 | |
| 866 | * Enables a [View Transition](https:
|
| 867 | * for this navigation.
|
| 868 | *
|
| 869 | * ```jsx
|
| 870 | * <Link to={to} viewTransition>
|
| 871 | * Click me
|
| 872 | * </Link>
|
| 873 | * ```
|
| 874 | *
|
| 875 | * To apply specific styles for the transition, see {@link useViewTransitionState}
|
| 876 | */
|
| 877 | viewTransition?: boolean;
|
| 878 | |
| 879 | * Specify the default revalidation behavior for the navigation.
|
| 880 | *
|
| 881 | * ```tsx
|
| 882 | * <Link to="/some/path" defaultShouldRevalidate={false} />
|
| 883 | * ```
|
| 884 | *
|
| 885 | * If no `shouldRevalidate` functions are present on the active routes, then this
|
| 886 | * value will be used directly. Otherwise it will be passed into `shouldRevalidate`
|
| 887 | * so the route can make the final determination on revalidation. This can be
|
| 888 | * useful when updating search params and you don't want to trigger a revalidation.
|
| 889 | *
|
| 890 | * By default (when not specified), loaders will revalidate according to the routers
|
| 891 | * standard revalidation behavior.
|
| 892 | */
|
| 893 | defaultShouldRevalidate?: boolean;
|
| 894 | |
| 895 | * Masked path for this navigation, when you want to navigate the router to
|
| 896 | * one location but display a separate location in the URL bar.
|
| 897 | *
|
| 898 | * This is useful for contextual navigations such as opening an image in a modal
|
| 899 | * on top of a gallery while keeping the underlying gallery active. If a user
|
| 900 | * shares the masked URL, or opens the link in a new tab, they will only load
|
| 901 | * the masked location without the underlying contextual location.
|
| 902 | *
|
| 903 | * This feature relies on `history.state` and is thus only intended for SPA uses
|
| 904 | * and SSR renders will not respect the masking.
|
| 905 | *
|
| 906 | * ```tsx
|
| 907 | *
|
| 908 | * export function clientLoader({ request }: Route.LoaderArgs) {
|
| 909 | * let sp = new URL(request.url).searchParams;
|
| 910 | * return {
|
| 911 | * images: getImages(),
|
| 912 | * modalImage: sp.has("image") ? getImage(sp.get("image")!) : null,
|
| 913 | * };
|
| 914 | * }
|
| 915 | *
|
| 916 | * export default function Gallery({ loaderData }: Route.ComponentProps) {
|
| 917 | * return (
|
| 918 | * <>
|
| 919 | * <GalleryGrid>
|
| 920 | * {loaderData.images.map((image) => (
|
| 921 | * <Link
|
| 922 | * key={image.id}
|
| 923 | * to={`/gallery?image=${image.id}`}
|
| 924 | * mask={`/images/${image.id}`}
|
| 925 | * >
|
| 926 | * <img src={image.url} alt={image.alt} />
|
| 927 | * </Link>
|
| 928 | * ))}
|
| 929 | * </GalleryGrid>
|
| 930 | *
|
| 931 | * {data.modalImage ? (
|
| 932 | * <dialog open>
|
| 933 | * <img src={data.modalImage.url} alt={data.modalImage.alt} />
|
| 934 | * </dialog>
|
| 935 | * ) : null}
|
| 936 | * </>
|
| 937 | * );
|
| 938 | * }
|
| 939 | * ```
|
| 940 | */
|
| 941 | mask?: To;
|
| 942 | }
|
| 943 | |
| 944 | * A progressively enhanced [`<a href>`](https:
|
| 945 | * wrapper to enable navigation with client-side routing.
|
| 946 | *
|
| 947 | * @example
|
| 948 | * import { Link } from "react-router";
|
| 949 | *
|
| 950 | * <Link to="/dashboard">Dashboard</Link>;
|
| 951 | *
|
| 952 | * <Link
|
| 953 | * to={{
|
| 954 | * pathname: "/some/path",
|
| 955 | * search: "?query=string",
|
| 956 | * hash: "#hash",
|
| 957 | * }}
|
| 958 | * />;
|
| 959 | *
|
| 960 | * @public
|
| 961 | * @category Components
|
| 962 | * @param {LinkProps.discover} props.discover [modes: framework] n/a
|
| 963 | * @param {LinkProps.prefetch} props.prefetch [modes: framework] n/a
|
| 964 | * @param {LinkProps.preventScrollReset} props.preventScrollReset [modes: framework, data] n/a
|
| 965 | * @param {LinkProps.relative} props.relative n/a
|
| 966 | * @param {LinkProps.reloadDocument} props.reloadDocument n/a
|
| 967 | * @param {LinkProps.replace} props.replace n/a
|
| 968 | * @param {LinkProps.state} props.state n/a
|
| 969 | * @param {LinkProps.to} props.to n/a
|
| 970 | * @param {LinkProps.viewTransition} props.viewTransition [modes: framework, data] n/a
|
| 971 | * @param {LinkProps.defaultShouldRevalidate} props.defaultShouldRevalidate n/a
|
| 972 | * @param {LinkProps.mask} props.mask [modes: framework, data] n/a
|
| 973 | */
|
| 974 | declare const Link: React$1.ForwardRefExoticComponent<LinkProps & React$1.RefAttributes<HTMLAnchorElement>>;
|
| 975 | |
| 976 | * The object passed to {@link NavLink} `children`, `className`, and `style` prop
|
| 977 | * callbacks to render and style the link based on its state.
|
| 978 | *
|
| 979 | * ```
|
| 980 | *
|
| 981 | * <NavLink
|
| 982 | * to="/messages"
|
| 983 | * className={({ isActive, isPending }) =>
|
| 984 | * isPending ? "pending" : isActive ? "active" : ""
|
| 985 | * }
|
| 986 | * >
|
| 987 | * Messages
|
| 988 | * </NavLink>
|
| 989 | *
|
| 990 | *
|
| 991 | * <NavLink
|
| 992 | * to="/messages"
|
| 993 | * style={({ isActive, isPending }) => {
|
| 994 | * return {
|
| 995 | * fontWeight: isActive ? "bold" : "",
|
| 996 | * color: isPending ? "red" : "black",
|
| 997 | * }
|
| 998 | * )}
|
| 999 | * />
|
| 1000 | *
|
| 1001 | *
|
| 1002 | * <NavLink to="/tasks">
|
| 1003 | * {({ isActive, isPending }) => (
|
| 1004 | * <span className={isActive ? "active" : ""}>Tasks</span>
|
| 1005 | * )}
|
| 1006 | * </NavLink>
|
| 1007 | * ```
|
| 1008 | *
|
| 1009 | */
|
| 1010 | type NavLinkRenderProps = {
|
| 1011 | |
| 1012 | * Indicates if the link's URL matches the current {@link Location}.
|
| 1013 | */
|
| 1014 | isActive: boolean;
|
| 1015 | |
| 1016 | * Indicates if the pending {@link Location} matches the link's URL. Only
|
| 1017 | * available in Framework/Data modes.
|
| 1018 | */
|
| 1019 | isPending: boolean;
|
| 1020 | |
| 1021 | * Indicates if a view transition to the link's URL is in progress.
|
| 1022 | * See {@link useViewTransitionState}
|
| 1023 | */
|
| 1024 | isTransitioning: boolean;
|
| 1025 | };
|
| 1026 | |
| 1027 | * @category Types
|
| 1028 | */
|
| 1029 | interface NavLinkProps extends Omit<LinkProps, "className" | "style" | "children"> {
|
| 1030 | |
| 1031 | * Can be regular React children or a function that receives an object with the
|
| 1032 | * `active` and `pending` states of the link.
|
| 1033 | *
|
| 1034 | * ```tsx
|
| 1035 | * <NavLink to="/tasks">
|
| 1036 | * {({ isActive }) => (
|
| 1037 | * <span className={isActive ? "active" : ""}>Tasks</span>
|
| 1038 | * )}
|
| 1039 | * </NavLink>
|
| 1040 | * ```
|
| 1041 | */
|
| 1042 | children?: React$1.ReactNode | ((props: NavLinkRenderProps) => React$1.ReactNode);
|
| 1043 | |
| 1044 | * Changes the matching logic to make it case-sensitive:
|
| 1045 | *
|
| 1046 | * | Link | URL | isActive |
|
| 1047 | * | -------------------------------------------- | ------------- | -------- |
|
| 1048 | * | `<NavLink to="/SpOnGe-bOB" />` | `/sponge-bob` | true |
|
| 1049 | * | `<NavLink to="/SpOnGe-bOB" caseSensitive />` | `/sponge-bob` | false |
|
| 1050 | */
|
| 1051 | caseSensitive?: boolean;
|
| 1052 | |
| 1053 | * Classes are automatically applied to `NavLink` that correspond to the state.
|
| 1054 | *
|
| 1055 | * ```css
|
| 1056 | * a.active {
|
| 1057 | * color: red;
|
| 1058 | * }
|
| 1059 | * a.pending {
|
| 1060 | * color: blue;
|
| 1061 | * }
|
| 1062 | * a.transitioning {
|
| 1063 | * view-transition-name: my-transition;
|
| 1064 | * }
|
| 1065 | * ```
|
| 1066 | *
|
| 1067 | * Or you can specify a function that receives {@link NavLinkRenderProps} and
|
| 1068 | * returns the `className`:
|
| 1069 | *
|
| 1070 | * ```tsx
|
| 1071 | * <NavLink className={({ isActive, isPending }) => (
|
| 1072 | * isActive ? "my-active-class" :
|
| 1073 | * isPending ? "my-pending-class" :
|
| 1074 | * ""
|
| 1075 | * )} />
|
| 1076 | * ```
|
| 1077 | */
|
| 1078 | className?: string | ((props: NavLinkRenderProps) => string | undefined);
|
| 1079 | |
| 1080 | * Changes the matching logic for the `active` and `pending` states to only match
|
| 1081 | * to the "end" of the {@link NavLinkProps.to}. If the URL is longer, it will no
|
| 1082 | * longer be considered active.
|
| 1083 | *
|
| 1084 | * | Link | URL | isActive |
|
| 1085 | * | ----------------------------- | ------------ | -------- |
|
| 1086 | * | `<NavLink to="/tasks" />` | `/tasks` | true |
|
| 1087 | * | `<NavLink to="/tasks" />` | `/tasks/123` | true |
|
| 1088 | * | `<NavLink to="/tasks" end />` | `/tasks` | true |
|
| 1089 | * | `<NavLink to="/tasks" end />` | `/tasks/123` | false |
|
| 1090 | *
|
| 1091 | * `<NavLink to="/">` is an exceptional case because _every_ URL matches `/`.
|
| 1092 | * To avoid this matching every single route by default, it effectively ignores
|
| 1093 | * the `end` prop and only matches when you're at the root route.
|
| 1094 | */
|
| 1095 | end?: boolean;
|
| 1096 | |
| 1097 | * Styles can also be applied dynamically via a function that receives
|
| 1098 | * {@link NavLinkRenderProps} and returns the styles:
|
| 1099 | *
|
| 1100 | * ```tsx
|
| 1101 | * <NavLink to="/tasks" style={{ color: "red" }} />
|
| 1102 | * <NavLink to="/tasks" style={({ isActive, isPending }) => ({
|
| 1103 | * color:
|
| 1104 | * isActive ? "red" :
|
| 1105 | * isPending ? "blue" : "black"
|
| 1106 | * })} />
|
| 1107 | * ```
|
| 1108 | */
|
| 1109 | style?: React$1.CSSProperties | ((props: NavLinkRenderProps) => React$1.CSSProperties | undefined);
|
| 1110 | }
|
| 1111 | |
| 1112 | * Wraps {@link Link | `<Link>`} with additional props for styling active and
|
| 1113 | * pending states.
|
| 1114 | *
|
| 1115 | * - Automatically applies classes to the link based on its `active` and `pending`
|
| 1116 | * states, see {@link NavLinkProps.className}
|
| 1117 | * - Note that `pending` is only available with Framework and Data modes.
|
| 1118 | * - Automatically applies `aria-current="page"` to the link when the link is active.
|
| 1119 | * See [`aria-current`](https:
|
| 1120 | * on MDN.
|
| 1121 | * - States are additionally available through the className, style, and children
|
| 1122 | * render props. See {@link NavLinkRenderProps}.
|
| 1123 | *
|
| 1124 | * @example
|
| 1125 | * <NavLink to="/message">Messages</NavLink>
|
| 1126 | *
|
| 1127 | *
|
| 1128 | * <NavLink
|
| 1129 | * to="/messages"
|
| 1130 | * className={({ isActive, isPending }) =>
|
| 1131 | * isPending ? "pending" : isActive ? "active" : ""
|
| 1132 | * }
|
| 1133 | * >
|
| 1134 | * Messages
|
| 1135 | * </NavLink>
|
| 1136 | *
|
| 1137 | * @public
|
| 1138 | * @category Components
|
| 1139 | * @param {NavLinkProps.caseSensitive} props.caseSensitive n/a
|
| 1140 | * @param {NavLinkProps.children} props.children n/a
|
| 1141 | * @param {NavLinkProps.className} props.className n/a
|
| 1142 | * @param {NavLinkProps.discover} props.discover [modes: framework] n/a
|
| 1143 | * @param {NavLinkProps.end} props.end n/a
|
| 1144 | * @param {NavLinkProps.prefetch} props.prefetch [modes: framework] n/a
|
| 1145 | * @param {NavLinkProps.preventScrollReset} props.preventScrollReset [modes: framework, data] n/a
|
| 1146 | * @param {NavLinkProps.relative} props.relative n/a
|
| 1147 | * @param {NavLinkProps.reloadDocument} props.reloadDocument n/a
|
| 1148 | * @param {NavLinkProps.replace} props.replace n/a
|
| 1149 | * @param {NavLinkProps.state} props.state n/a
|
| 1150 | * @param {NavLinkProps.style} props.style n/a
|
| 1151 | * @param {NavLinkProps.to} props.to n/a
|
| 1152 | * @param {NavLinkProps.viewTransition} props.viewTransition [modes: framework, data] n/a
|
| 1153 | */
|
| 1154 | declare const NavLink: React$1.ForwardRefExoticComponent<NavLinkProps & React$1.RefAttributes<HTMLAnchorElement>>;
|
| 1155 | |
| 1156 | * Form props shared by navigations and fetchers
|
| 1157 | */
|
| 1158 | interface SharedFormProps extends React$1.FormHTMLAttributes<HTMLFormElement> {
|
| 1159 | |
| 1160 | * The HTTP verb to use when the form is submitted. Supports `"delete"`,
|
| 1161 | * `"get"`, `"patch"`, `"post"`, and `"put"`.
|
| 1162 | *
|
| 1163 | * Native [`<form>`](https:
|
| 1164 | * only supports `"get"` and `"post"`, avoid the other verbs if you'd like to
|
| 1165 | * support progressive enhancement
|
| 1166 | */
|
| 1167 | method?: HTMLFormMethod;
|
| 1168 | |
| 1169 | * The encoding type to use for the form submission.
|
| 1170 | *
|
| 1171 | * ```tsx
|
| 1172 | * <Form encType="application/x-www-form-urlencoded"/>
|
| 1173 | * <Form encType="multipart/form-data"/>
|
| 1174 | * <Form encType="text/plain"/>
|
| 1175 | * ```
|
| 1176 | */
|
| 1177 | encType?: "application/x-www-form-urlencoded" | "multipart/form-data" | "text/plain";
|
| 1178 | |
| 1179 | * The URL to submit the form data to. If `undefined`, this defaults to the
|
| 1180 | * closest route in context.
|
| 1181 | */
|
| 1182 | action?: string;
|
| 1183 | |
| 1184 | * Determines whether the form action is relative to the route hierarchy or
|
| 1185 | * the pathname. Use this if you want to opt out of navigating the route
|
| 1186 | * hierarchy and want to instead route based on slash-delimited URL segments.
|
| 1187 | * See {@link RelativeRoutingType}.
|
| 1188 | */
|
| 1189 | relative?: RelativeRoutingType;
|
| 1190 | |
| 1191 | * Prevent the scroll position from resetting to the top of the viewport on
|
| 1192 | * completion of the navigation when using the
|
| 1193 | * {@link ScrollRestoration | `<ScrollRestoration>`} component
|
| 1194 | */
|
| 1195 | preventScrollReset?: boolean;
|
| 1196 | |
| 1197 | * Specify the default revalidation behavior after this submission
|
| 1198 | *
|
| 1199 | * If no `shouldRevalidate` functions are present on the active routes, then this
|
| 1200 | * value will be used directly. Otherwise it will be passed into `shouldRevalidate`
|
| 1201 | * so the route can make the final determination on revalidation. This can be
|
| 1202 | * useful when updating search params and you don't want to trigger a revalidation.
|
| 1203 | *
|
| 1204 | * By default (when not specified), loaders will revalidate according to the routers
|
| 1205 | * standard revalidation behavior.
|
| 1206 | */
|
| 1207 | defaultShouldRevalidate?: boolean;
|
| 1208 | }
|
| 1209 | |
| 1210 | * Form props available to fetchers
|
| 1211 | * @category Types
|
| 1212 | */
|
| 1213 | interface FetcherFormProps extends SharedFormProps {}
|
| 1214 | |
| 1215 | * Form props available to navigations
|
| 1216 | * @category Types
|
| 1217 | */
|
| 1218 | interface FormProps extends SharedFormProps {
|
| 1219 | |
| 1220 | * Defines the form [lazy route discovery](../../explanation/lazy-route-discovery) behavior.
|
| 1221 | *
|
| 1222 | * - **render** — default, discover the route when the form renders
|
| 1223 | * - **none** — don't eagerly discover, only discover if the form is submitted
|
| 1224 | *
|
| 1225 | * ```tsx
|
| 1226 | * <Form />
|
| 1227 | * <Form discover="render" />
|
| 1228 | * <Form discover="none" />
|
| 1229 | * ```
|
| 1230 | */
|
| 1231 | discover?: DiscoverBehavior;
|
| 1232 | |
| 1233 | * Indicates a specific fetcherKey to use when using `navigate={false}` so you
|
| 1234 | * can pick up the fetcher's state in a different component in a {@link useFetcher}.
|
| 1235 | */
|
| 1236 | fetcherKey?: string;
|
| 1237 | |
| 1238 | * When `false`, skips the navigation and submits via a fetcher internally.
|
| 1239 | * This is essentially a shorthand for {@link useFetcher} + `<fetcher.Form>` where
|
| 1240 | * you don't care about the resulting data in this component.
|
| 1241 | */
|
| 1242 | navigate?: boolean;
|
| 1243 | |
| 1244 | * Forces a full document navigation instead of client side routing and data
|
| 1245 | * fetch.
|
| 1246 | */
|
| 1247 | reloadDocument?: boolean;
|
| 1248 | |
| 1249 | * Replaces the current entry in the browser [`History`](https:
|
| 1250 | * stack when the form navigates. Use this if you don't want the user to be
|
| 1251 | * able to click "back" to the page with the form on it.
|
| 1252 | */
|
| 1253 | replace?: boolean;
|
| 1254 | |
| 1255 | * State object to add to the [`History`](https:
|
| 1256 | * stack entry for this navigation
|
| 1257 | */
|
| 1258 | state?: any;
|
| 1259 | |
| 1260 | * Enables a [View Transition](https:
|
| 1261 | * for this navigation. To apply specific styles during the transition, see
|
| 1262 | * {@link useViewTransitionState}.
|
| 1263 | */
|
| 1264 | viewTransition?: boolean;
|
| 1265 | }
|
| 1266 | |
| 1267 | * A progressively enhanced HTML [`<form>`](https:
|
| 1268 | * that submits data to actions via [`fetch`](https:
|
| 1269 | * activating pending states in {@link useNavigation} which enables advanced
|
| 1270 | * user interfaces beyond a basic HTML [`<form>`](https:
|
| 1271 | * After a form's `action` completes, all data on the page is automatically
|
| 1272 | * revalidated to keep the UI in sync with the data.
|
| 1273 | *
|
| 1274 | * Because it uses the HTML form API, server rendered pages are interactive at a
|
| 1275 | * basic level before JavaScript loads. Instead of React Router managing the
|
| 1276 | * submission, the browser manages the submission as well as the pending states
|
| 1277 | * (like the spinning favicon). After JavaScript loads, React Router takes over
|
| 1278 | * enabling web application user experiences.
|
| 1279 | *
|
| 1280 | * `Form` is most useful for submissions that should also change the URL or
|
| 1281 | * otherwise add an entry to the browser history stack. For forms that shouldn't
|
| 1282 | * manipulate the browser [`History`](https:
|
| 1283 | * stack, use {@link FetcherWithComponents.Form | `<fetcher.Form>`}.
|
| 1284 | *
|
| 1285 | * @example
|
| 1286 | * import { Form } from "react-router";
|
| 1287 | *
|
| 1288 | * function NewEvent() {
|
| 1289 | * return (
|
| 1290 | * <Form action="/events" method="post">
|
| 1291 | * <input name="title" type="text" />
|
| 1292 | * <input name="description" type="text" />
|
| 1293 | * </Form>
|
| 1294 | * );
|
| 1295 | * }
|
| 1296 | *
|
| 1297 | * @public
|
| 1298 | * @category Components
|
| 1299 | * @mode framework
|
| 1300 | * @mode data
|
| 1301 | * @param {FormProps.action} action n/a
|
| 1302 | * @param {FormProps.discover} discover n/a
|
| 1303 | * @param {FormProps.encType} encType n/a
|
| 1304 | * @param {FormProps.fetcherKey} fetcherKey n/a
|
| 1305 | * @param {FormProps.method} method n/a
|
| 1306 | * @param {FormProps.navigate} navigate n/a
|
| 1307 | * @param {FormProps.preventScrollReset} preventScrollReset n/a
|
| 1308 | * @param {FormProps.relative} relative n/a
|
| 1309 | * @param {FormProps.reloadDocument} reloadDocument n/a
|
| 1310 | * @param {FormProps.replace} replace n/a
|
| 1311 | * @param {FormProps.state} state n/a
|
| 1312 | * @param {FormProps.viewTransition} viewTransition n/a
|
| 1313 | * @param {FormProps.defaultShouldRevalidate} defaultShouldRevalidate n/a
|
| 1314 | * @returns A progressively enhanced [`<form>`](https:
|
| 1315 | */
|
| 1316 | declare const Form: React$1.ForwardRefExoticComponent<FormProps & React$1.RefAttributes<HTMLFormElement>>;
|
| 1317 | type ScrollRestorationProps = ScriptsProps & {
|
| 1318 | |
| 1319 | * A function that returns a key to use for scroll restoration. This is useful
|
| 1320 | * for custom scroll restoration logic, such as using only the pathname so
|
| 1321 | * that later navigations to prior paths will restore the scroll. Defaults to
|
| 1322 | * `location.key`. See {@link GetScrollRestorationKeyFunction}.
|
| 1323 | *
|
| 1324 | * ```tsx
|
| 1325 | * <ScrollRestoration
|
| 1326 | * getKey={(location, matches) => {
|
| 1327 | *
|
| 1328 | * return location.key
|
| 1329 | *
|
| 1330 | *
|
| 1331 | * return location.pathname
|
| 1332 | * }}
|
| 1333 | * />
|
| 1334 | * ```
|
| 1335 | */
|
| 1336 | getKey?: GetScrollRestorationKeyFunction;
|
| 1337 | |
| 1338 | * The key to use for storing scroll positions in [`sessionStorage`](https:
|
| 1339 | * Defaults to `"react-router-scroll-positions"`.
|
| 1340 | */
|
| 1341 | storageKey?: string;
|
| 1342 | };
|
| 1343 | |
| 1344 | * Emulates the browser's scroll restoration on location changes. Apps should only render one of these, right before the {@link Scripts} component.
|
| 1345 | *
|
| 1346 | * ```tsx
|
| 1347 | * import { ScrollRestoration } from "react-router";
|
| 1348 | *
|
| 1349 | * export default function Root() {
|
| 1350 | * return (
|
| 1351 | * <html>
|
| 1352 | * <body>
|
| 1353 | * <ScrollRestoration />
|
| 1354 | * <Scripts />
|
| 1355 | * </body>
|
| 1356 | * </html>
|
| 1357 | * );
|
| 1358 | * }
|
| 1359 | * ```
|
| 1360 | *
|
| 1361 | * This component renders an inline `<script>` to prevent scroll flashing. The
|
| 1362 | * `nonce` prop will be passed down to the script tag to allow CSP nonce usage.
|
| 1363 | * If not provided in Framework Mode, it will default to any
|
| 1364 | * {@link ServerRouter | `<ServerRouter nonce>`} prop.
|
| 1365 | *
|
| 1366 | * ```tsx
|
| 1367 | * <ScrollRestoration nonce={cspNonce} />
|
| 1368 | * ```
|
| 1369 | *
|
| 1370 | * @public
|
| 1371 | * @category Components
|
| 1372 | * @mode framework
|
| 1373 | * @mode data
|
| 1374 | * @param props Props
|
| 1375 | * @param {ScrollRestorationProps.getKey} props.getKey n/a
|
| 1376 | * @param {ScriptsProps.nonce} props.nonce n/a
|
| 1377 | * @param {ScrollRestorationProps.storageKey} props.storageKey n/a
|
| 1378 | * @returns A [`<script>`](https:
|
| 1379 | * tag that restores scroll positions on navigation.
|
| 1380 | */
|
| 1381 | declare function ScrollRestoration({
|
| 1382 | getKey,
|
| 1383 | storageKey,
|
| 1384 | ...props
|
| 1385 | }: ScrollRestorationProps): React$1.JSX.Element | null;
|
| 1386 | declare namespace ScrollRestoration {
|
| 1387 | var displayName: string;
|
| 1388 | }
|
| 1389 | |
| 1390 | * Handles the click behavior for router {@link Link | `<Link>`} components.This
|
| 1391 | * is useful if you need to create custom {@link Link | `<Link>`} components with
|
| 1392 | * the same click behavior we use in our exported {@link Link | `<Link>`}.
|
| 1393 | *
|
| 1394 | * @public
|
| 1395 | * @category Hooks
|
| 1396 | * @param to The URL to navigate to, can be a string or a partial {@link Path}.
|
| 1397 | * @param options Options
|
| 1398 | * @param options.preventScrollReset Whether to prevent the scroll position from
|
| 1399 | * being reset to the top of the viewport on completion of the navigation when
|
| 1400 | * using the {@link ScrollRestoration} component. Defaults to `false`.
|
| 1401 | * @param options.relative The {@link RelativeRoutingType | relative routing type}
|
| 1402 | * to use for the link. Defaults to `"route"`.
|
| 1403 | * @param options.replace Whether to replace the current [`History`](https:
|
| 1404 | * entry instead of pushing a new one. Defaults to `false`.
|
| 1405 | * @param options.state The state to add to the [`History`](https:
|
| 1406 | * entry for this navigation. Defaults to `undefined`.
|
| 1407 | * @param options.target The target attribute for the link. Defaults to `undefined`.
|
| 1408 | * @param options.viewTransition Enables a [View Transition](https:
|
| 1409 | * for this navigation. To apply specific styles during the transition, see
|
| 1410 | * {@link useViewTransitionState}. Defaults to `false`.
|
| 1411 | * @param options.defaultShouldRevalidate Specify the default revalidation
|
| 1412 | * behavior for the navigation. When not specified, loaders revalidate
|
| 1413 | * according to the router's standard revalidation behavior.
|
| 1414 | * @param options.mask Masked location to display in the browser instead
|
| 1415 | * of the router location. Defaults to `undefined`.
|
| 1416 | * @param options.useTransitions Wraps the navigation in
|
| 1417 | * [`React.startTransition`](https:
|
| 1418 | * for concurrent rendering. Defaults to `false`.
|
| 1419 | * @returns A click handler function that can be used in a custom {@link Link} component.
|
| 1420 | */
|
| 1421 | declare function useLinkClickHandler<E extends Element = HTMLAnchorElement>(to: To, {
|
| 1422 | target,
|
| 1423 | replace: replaceProp,
|
| 1424 | mask,
|
| 1425 | state,
|
| 1426 | preventScrollReset,
|
| 1427 | relative,
|
| 1428 | viewTransition,
|
| 1429 | defaultShouldRevalidate,
|
| 1430 | useTransitions
|
| 1431 | }?: {
|
| 1432 | target?: React$1.HTMLAttributeAnchorTarget;
|
| 1433 | replace?: boolean;
|
| 1434 | mask?: To;
|
| 1435 | state?: any;
|
| 1436 | preventScrollReset?: boolean;
|
| 1437 | relative?: RelativeRoutingType;
|
| 1438 | viewTransition?: boolean;
|
| 1439 | defaultShouldRevalidate?: boolean;
|
| 1440 | useTransitions?: boolean;
|
| 1441 | }): (event: React$1.MouseEvent<E, MouseEvent>) => void;
|
| 1442 | |
| 1443 | * Returns a tuple of the current URL's [`URLSearchParams`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams)
|
| 1444 | * and a function to update them. Setting the search params causes a navigation.
|
| 1445 | *
|
| 1446 | * ```tsx
|
| 1447 | * import { useSearchParams } from "react-router";
|
| 1448 | *
|
| 1449 | * export function SomeComponent() {
|
| 1450 | * const [searchParams, setSearchParams] = useSearchParams();
|
| 1451 | *
|
| 1452 | * }
|
| 1453 | * ```
|
| 1454 | *
|
| 1455 | * ### `setSearchParams` function
|
| 1456 | *
|
| 1457 | * The second element of the tuple is a function that can be used to update the
|
| 1458 | * search params. It accepts the same types as `defaultInit` and will cause a
|
| 1459 | * navigation to the new URL.
|
| 1460 | *
|
| 1461 | * ```tsx
|
| 1462 | * let [searchParams, setSearchParams] = useSearchParams();
|
| 1463 | *
|
| 1464 | *
|
| 1465 | * setSearchParams("?tab=1");
|
| 1466 | *
|
| 1467 | *
|
| 1468 | * setSearchParams({ tab: "1" });
|
| 1469 | *
|
| 1470 | *
|
| 1471 | * setSearchParams({ brand: ["nike", "reebok"] });
|
| 1472 | *
|
| 1473 | *
|
| 1474 | * setSearchParams([["tab", "1"]]);
|
| 1475 | *
|
| 1476 | *
|
| 1477 | * setSearchParams(new URLSearchParams("?tab=1"));
|
| 1478 | * ```
|
| 1479 | *
|
| 1480 | * It also supports a function callback like React's
|
| 1481 | * [`setState`](https:
|
| 1482 | *
|
| 1483 | * ```tsx
|
| 1484 | * setSearchParams((searchParams) => {
|
| 1485 | * searchParams.set("tab", "2");
|
| 1486 | * return searchParams;
|
| 1487 | * });
|
| 1488 | * ```
|
| 1489 | *
|
| 1490 | * <docs-warning>The function callback version of `setSearchParams` does not support
|
| 1491 | * the [queueing](https:
|
| 1492 | * logic that React's `setState` implements. Multiple calls to `setSearchParams`
|
| 1493 | * in the same tick will not build on the prior value. If you need this behavior,
|
| 1494 | * you can use `setState` manually.</docs-warning>
|
| 1495 | *
|
| 1496 | * ### Notes
|
| 1497 | *
|
| 1498 | * Note that `searchParams` is a stable reference, so you can reliably use it
|
| 1499 | * as a dependency in React's [`useEffect`](https://react.dev/reference/react/useEffect)
|
| 1500 | * hooks.
|
| 1501 | *
|
| 1502 | * ```tsx
|
| 1503 | * useEffect(() => {
|
| 1504 | * console.log(searchParams.get("tab"));
|
| 1505 | * }, [searchParams]);
|
| 1506 | * ```
|
| 1507 | *
|
| 1508 | * However, this also means it's mutable. If you change the object without
|
| 1509 | * calling `setSearchParams`, its values will change between renders if some
|
| 1510 | * other state causes the component to re-render and URL will not reflect the
|
| 1511 | * values.
|
| 1512 | *
|
| 1513 | * @public
|
| 1514 | * @category Hooks
|
| 1515 | * @param defaultInit
|
| 1516 | * You can initialize the search params with a default value, though it **will
|
| 1517 | * not** change the URL on the first render.
|
| 1518 | *
|
| 1519 | * ```tsx
|
| 1520 | *
|
| 1521 | * useSearchParams("?tab=1");
|
| 1522 | *
|
| 1523 | *
|
| 1524 | * useSearchParams({ tab: "1" });
|
| 1525 | *
|
| 1526 | *
|
| 1527 | * useSearchParams({ brand: ["nike", "reebok"] });
|
| 1528 | *
|
| 1529 | *
|
| 1530 | * useSearchParams([["tab", "1"]]);
|
| 1531 | *
|
| 1532 | *
|
| 1533 | * useSearchParams(new URLSearchParams("?tab=1"));
|
| 1534 | * ```
|
| 1535 | * @returns A tuple of the current [`URLSearchParams`](https:
|
| 1536 | * and a function to update them.
|
| 1537 | */
|
| 1538 | declare function useSearchParams(defaultInit?: URLSearchParamsInit): [URLSearchParams, SetURLSearchParams];
|
| 1539 | |
| 1540 | * Sets new search params and causes a navigation when called.
|
| 1541 | *
|
| 1542 | * ```tsx
|
| 1543 | * <button
|
| 1544 | * onClick={() => {
|
| 1545 | * const params = new URLSearchParams();
|
| 1546 | * params.set("someKey", "someValue");
|
| 1547 | * setSearchParams(params, {
|
| 1548 | * preventScrollReset: true,
|
| 1549 | * });
|
| 1550 | * }}
|
| 1551 | * />
|
| 1552 | * ```
|
| 1553 | *
|
| 1554 | * It also supports a function for setting new search params.
|
| 1555 | *
|
| 1556 | * ```tsx
|
| 1557 | * <button
|
| 1558 | * onClick={() => {
|
| 1559 | * setSearchParams((prev) => {
|
| 1560 | * prev.set("someKey", "someValue");
|
| 1561 | * return prev;
|
| 1562 | * });
|
| 1563 | * }}
|
| 1564 | * />
|
| 1565 | * ```
|
| 1566 | */
|
| 1567 | type SetURLSearchParams = (nextInit?: URLSearchParamsInit | ((prev: URLSearchParams) => URLSearchParamsInit), navigateOpts?: NavigateOptions) => void;
|
| 1568 | |
| 1569 | * Submits a HTML [`<form>`](https:
|
| 1570 | * to the server without reloading the page.
|
| 1571 | */
|
| 1572 | interface SubmitFunction {
|
| 1573 | (
|
| 1574 | |
| 1575 | * Can be multiple types of elements and objects
|
| 1576 | *
|
| 1577 | * **[`HTMLFormElement`](https:
|
| 1578 | *
|
| 1579 | * ```tsx
|
| 1580 | * <Form
|
| 1581 | * onSubmit={(event) => {
|
| 1582 | * submit(event.currentTarget);
|
| 1583 | * }}
|
| 1584 | * />
|
| 1585 | * ```
|
| 1586 | *
|
| 1587 | * **[`FormData`](https:
|
| 1588 | *
|
| 1589 | * ```tsx
|
| 1590 | * const formData = new FormData();
|
| 1591 | * formData.append("myKey", "myValue");
|
| 1592 | * submit(formData, { method: "post" });
|
| 1593 | * ```
|
| 1594 | *
|
| 1595 | * **Plain object that will be serialized as [`FormData`](https:
|
| 1596 | *
|
| 1597 | * ```tsx
|
| 1598 | * submit({ myKey: "myValue" }, { method: "post" });
|
| 1599 | * ```
|
| 1600 | *
|
| 1601 | * **Plain object that will be serialized as JSON**
|
| 1602 | *
|
| 1603 | * ```tsx
|
| 1604 | * submit(
|
| 1605 | * { myKey: "myValue" },
|
| 1606 | * { method: "post", encType: "application/json" }
|
| 1607 | * );
|
| 1608 | * ```
|
| 1609 | */
|
| 1610 | target: SubmitTarget,
|
| 1611 | |
| 1612 | * Options that override the [`<form>`](https:
|
| 1613 | * own attributes. Required when submitting arbitrary data without a backing
|
| 1614 | * [`<form>`](https:
|
| 1615 | */
|
| 1616 | options?: SubmitOptions): Promise<void>;
|
| 1617 | }
|
| 1618 | |
| 1619 | * Submits a fetcher [`<form>`](https:
|
| 1620 | */
|
| 1621 | interface FetcherSubmitFunction {
|
| 1622 | (
|
| 1623 | |
| 1624 | * Can be multiple types of elements and objects
|
| 1625 | *
|
| 1626 | * **[`HTMLFormElement`](https:
|
| 1627 | *
|
| 1628 | * ```tsx
|
| 1629 | * <fetcher.Form
|
| 1630 | * onSubmit={(event) => {
|
| 1631 | * fetcher.submit(event.currentTarget);
|
| 1632 | * }}
|
| 1633 | * />
|
| 1634 | * ```
|
| 1635 | *
|
| 1636 | * **[`FormData`](https:
|
| 1637 | *
|
| 1638 | * ```tsx
|
| 1639 | * const formData = new FormData();
|
| 1640 | * formData.append("myKey", "myValue");
|
| 1641 | * fetcher.submit(formData, { method: "post" });
|
| 1642 | * ```
|
| 1643 | *
|
| 1644 | * **Plain object that will be serialized as [`FormData`](https:
|
| 1645 | *
|
| 1646 | * ```tsx
|
| 1647 | * fetcher.submit({ myKey: "myValue" }, { method: "post" });
|
| 1648 | * ```
|
| 1649 | *
|
| 1650 | * **Plain object that will be serialized as JSON**
|
| 1651 | *
|
| 1652 | * ```tsx
|
| 1653 | * fetcher.submit(
|
| 1654 | * { myKey: "myValue" },
|
| 1655 | * { method: "post", encType: "application/json" }
|
| 1656 | * );
|
| 1657 | * ```
|
| 1658 | */
|
| 1659 | target: SubmitTarget, options?: FetcherSubmitOptions): Promise<void>;
|
| 1660 | }
|
| 1661 | |
| 1662 | * The imperative version of {@link Form | `<Form>`} that lets you submit a form
|
| 1663 | * from code instead of a user interaction.
|
| 1664 | *
|
| 1665 | * @example
|
| 1666 | * import { useSubmit } from "react-router";
|
| 1667 | *
|
| 1668 | * function SomeComponent() {
|
| 1669 | * const submit = useSubmit();
|
| 1670 | * return (
|
| 1671 | * <Form onChange={(event) => submit(event.currentTarget)} />
|
| 1672 | * );
|
| 1673 | * }
|
| 1674 | *
|
| 1675 | * @public
|
| 1676 | * @category Hooks
|
| 1677 | * @mode framework
|
| 1678 | * @mode data
|
| 1679 | * @returns A function that can be called to submit a {@link Form} imperatively.
|
| 1680 | */
|
| 1681 | declare function useSubmit(): SubmitFunction;
|
| 1682 | |
| 1683 | * Resolves the URL to the closest route in the component hierarchy instead of
|
| 1684 | * the current URL of the app.
|
| 1685 | *
|
| 1686 | * This is used internally by {@link Form} to resolve the `action` to the closest
|
| 1687 | * route, but can be used generically as well.
|
| 1688 | *
|
| 1689 | * ```ts
|
| 1690 | * import { useFormAction } from "react-router";
|
| 1691 | *
|
| 1692 | * function SomeComponent() {
|
| 1693 | *
|
| 1694 | * let action = useFormAction();
|
| 1695 | *
|
| 1696 | *
|
| 1697 | * let destroyAction = useFormAction("destroy");
|
| 1698 | * }
|
| 1699 | * ```
|
| 1700 | *
|
| 1701 | * <docs-info>This hook adds a `basename` if your app specifies one, so that it
|
| 1702 | * can be used with raw `<form>` elements in a progressively enhanced way. If
|
| 1703 | * you are using this to provide an `action` to `<Form>` or `fetcher.submit`, you
|
| 1704 | * will need to remove the `basename` since both of those will prepend it
|
| 1705 | * internally.</docs-info>
|
| 1706 | *
|
| 1707 | *
|
| 1708 | * @public
|
| 1709 | * @category Hooks
|
| 1710 | * @mode framework
|
| 1711 | * @mode data
|
| 1712 | * @param action The action to append to the closest route URL. Defaults to the
|
| 1713 | * closest route URL.
|
| 1714 | * @param options Options
|
| 1715 | * @param options.relative The relative routing type to use when resolving the
|
| 1716 | * action. Defaults to `"route"`.
|
| 1717 | * @returns The resolved action URL.
|
| 1718 | */
|
| 1719 | declare function useFormAction(action?: string, {
|
| 1720 | relative
|
| 1721 | }?: {
|
| 1722 | relative?: RelativeRoutingType;
|
| 1723 | }): string;
|
| 1724 | |
| 1725 | * The return value {@link useFetcher} that keeps track of the state of a fetcher.
|
| 1726 | *
|
| 1727 | * ```tsx
|
| 1728 | * let fetcher = useFetcher();
|
| 1729 | * ```
|
| 1730 | */
|
| 1731 | type FetcherWithComponents<TData> = Fetcher<TData> & {
|
| 1732 | |
| 1733 | * Just like {@link Form} except it doesn't cause a navigation.
|
| 1734 | *
|
| 1735 | * ```tsx
|
| 1736 | * function SomeComponent() {
|
| 1737 | * const fetcher = useFetcher()
|
| 1738 | * return (
|
| 1739 | * <fetcher.Form method="post" action="/some/route">
|
| 1740 | * <input type="text" />
|
| 1741 | * </fetcher.Form>
|
| 1742 | * )
|
| 1743 | * }
|
| 1744 | * ```
|
| 1745 | */
|
| 1746 | Form: React$1.ForwardRefExoticComponent<FetcherFormProps & React$1.RefAttributes<HTMLFormElement>>;
|
| 1747 | |
| 1748 | * Loads data from a route. Useful for loading data imperatively inside user
|
| 1749 | * events outside a normal button or form, like a combobox or search input.
|
| 1750 | *
|
| 1751 | * ```tsx
|
| 1752 | * let fetcher = useFetcher()
|
| 1753 | *
|
| 1754 | * <input onChange={e => {
|
| 1755 | * fetcher.load(`/search?q=${e.target.value}`)
|
| 1756 | * }} />
|
| 1757 | * ```
|
| 1758 | */
|
| 1759 | load: (href: string, opts?: {
|
| 1760 | |
| 1761 | * Wraps the initial state update for this `fetcher.load` in a
|
| 1762 | * [`ReactDOM.flushSync`](https:
|
| 1763 | * call instead of the default [`React.startTransition`](https:
|
| 1764 | * This allows you to perform synchronous DOM actions immediately after the
|
| 1765 | * update is flushed to the DOM.
|
| 1766 | */
|
| 1767 | flushSync?: boolean;
|
| 1768 | }) => Promise<void>;
|
| 1769 | |
| 1770 | * Reset a fetcher back to an empty/idle state.
|
| 1771 | *
|
| 1772 | * If the fetcher is currently in-flight, the
|
| 1773 | * [`AbortController`](https:
|
| 1774 | * will be aborted with the `reason`, if provided.
|
| 1775 | * @param opts Options for resetting the fetcher.
|
| 1776 | * @param opts.reason Optional `reason` to provide to [`AbortController.abort()`](https:
|
| 1777 | * @returns void
|
| 1778 | */
|
| 1779 | reset: (opts?: {
|
| 1780 | reason?: unknown;
|
| 1781 | }) => void;
|
| 1782 | |
| 1783 | * Submits form data to a route. While multiple nested routes can match a URL, only the leaf route will be called.
|
| 1784 | *
|
| 1785 | * The `formData` can be multiple types:
|
| 1786 | *
|
| 1787 | * - [`FormData`](https:
|
| 1788 | * A `FormData` instance.
|
| 1789 | * - [`HTMLFormElement`](https:
|
| 1790 | * A [`<form>`](https:
|
| 1791 | * - `Object`
|
| 1792 | * An object of key/value-pairs that will be converted to a [`FormData`](https:
|
| 1793 | * instance by default. You can pass a more complex object and serialize it
|
| 1794 | * as JSON by specifying `encType: "application/json"`. See
|
| 1795 | * {@link useSubmit} for more details.
|
| 1796 | *
|
| 1797 | * If the method is `GET`, then the route [`loader`](../../start/framework/route-module#loader)
|
| 1798 | * is being called and with the `formData` serialized to the url as [`URLSearchParams`](https:
|
| 1799 | * If `DELETE`, `PATCH`, `POST`, or `PUT`, then the route [`action`](../../start/framework/route-module#action)
|
| 1800 | * is being called with `formData` as the body.
|
| 1801 | *
|
| 1802 | * ```tsx
|
| 1803 | *
|
| 1804 | * const formData = new FormData();
|
| 1805 | * fetcher.submit(formData);
|
| 1806 | *
|
| 1807 | *
|
| 1808 | * fetcher.submit(event.currentTarget.form, {
|
| 1809 | * method: "POST",
|
| 1810 | * });
|
| 1811 | *
|
| 1812 | *
|
| 1813 | * fetcher.submit(
|
| 1814 | * { serialized: "values" },
|
| 1815 | * { method: "POST" }
|
| 1816 | * );
|
| 1817 | *
|
| 1818 | *
|
| 1819 | * fetcher.submit(
|
| 1820 | * {
|
| 1821 | * deeply: {
|
| 1822 | * nested: {
|
| 1823 | * json: "values",
|
| 1824 | * },
|
| 1825 | * },
|
| 1826 | * },
|
| 1827 | * {
|
| 1828 | * method: "POST",
|
| 1829 | * encType: "application/json",
|
| 1830 | * }
|
| 1831 | * );
|
| 1832 | * ```
|
| 1833 | */
|
| 1834 | submit: FetcherSubmitFunction;
|
| 1835 | };
|
| 1836 | |
| 1837 | * Useful for creating complex, dynamic user interfaces that require multiple,
|
| 1838 | * concurrent data interactions without causing a navigation.
|
| 1839 | *
|
| 1840 | * Fetchers track their own, independent state and can be used to load data, submit
|
| 1841 | * forms, and generally interact with [`action`](../../start/framework/route-module#action)
|
| 1842 | * and [`loader`](../../start/framework/route-module#loader) functions.
|
| 1843 | *
|
| 1844 | * @example
|
| 1845 | * import { useFetcher } from "react-router"
|
| 1846 | *
|
| 1847 | * function SomeComponent() {
|
| 1848 | * let fetcher = useFetcher()
|
| 1849 | *
|
| 1850 | *
|
| 1851 | * fetcher.state
|
| 1852 | * fetcher.data
|
| 1853 | *
|
| 1854 | *
|
| 1855 | * <fetcher.Form method="post" />
|
| 1856 | *
|
| 1857 | *
|
| 1858 | * fetcher.load("/some/route")
|
| 1859 | *
|
| 1860 | *
|
| 1861 | * fetcher.submit(someFormRef, { method: "post" })
|
| 1862 | * fetcher.submit(someData, {
|
| 1863 | * method: "post",
|
| 1864 | * encType: "application/json"
|
| 1865 | * })
|
| 1866 | *
|
| 1867 | *
|
| 1868 | * fetcher.reset()
|
| 1869 | * }
|
| 1870 | *
|
| 1871 | * @public
|
| 1872 | * @category Hooks
|
| 1873 | * @mode framework
|
| 1874 | * @mode data
|
| 1875 | * @param options Options
|
| 1876 | * @param options.key A unique key to identify the fetcher.
|
| 1877 | *
|
| 1878 | *
|
| 1879 | * By default, `useFetcher` generates a unique fetcher scoped to that component.
|
| 1880 | * If you want to identify a fetcher with your own key such that you can access
|
| 1881 | * it from elsewhere in your app, you can do that with the `key` option:
|
| 1882 | *
|
| 1883 | * ```tsx
|
| 1884 | * function SomeComp() {
|
| 1885 | * let fetcher = useFetcher({ key: "my-key" })
|
| 1886 | *
|
| 1887 | * }
|
| 1888 | *
|
| 1889 | *
|
| 1890 | * function AnotherComp() {
|
| 1891 | *
|
| 1892 | * let fetcher = useFetcher({ key: "my-key" });
|
| 1893 | *
|
| 1894 | * }
|
| 1895 | * ```
|
| 1896 | * @returns A {@link FetcherWithComponents} object that contains the fetcher's state, data, and components for submitting forms and loading data.
|
| 1897 | */
|
| 1898 | declare function useFetcher<T = any>({
|
| 1899 | key
|
| 1900 | }?: {
|
| 1901 | key?: string;
|
| 1902 | }): FetcherWithComponents<SerializeFrom<T>>;
|
| 1903 | |
| 1904 | * Returns an array of all in-flight {@link Fetcher}s. This is useful for components
|
| 1905 | * throughout the app that didn't create the fetchers but want to use their submissions
|
| 1906 | * to participate in optimistic UI.
|
| 1907 | *
|
| 1908 | * @example
|
| 1909 | * import { useFetchers } from "react-router";
|
| 1910 | *
|
| 1911 | * function SomeComponent() {
|
| 1912 | * const fetchers = useFetchers();
|
| 1913 | * fetchers[0].formData;
|
| 1914 | * fetchers[0].state;
|
| 1915 | *
|
| 1916 | * }
|
| 1917 | *
|
| 1918 | * @public
|
| 1919 | * @category Hooks
|
| 1920 | * @mode framework
|
| 1921 | * @mode data
|
| 1922 | * @returns An array of all in-flight {@link Fetcher}s, each with a unique `key`
|
| 1923 | * property.
|
| 1924 | */
|
| 1925 | declare function useFetchers(): (Fetcher & {
|
| 1926 | key: string;
|
| 1927 | })[];
|
| 1928 | |
| 1929 | * When rendered inside a {@link RouterProvider}, will restore scroll positions
|
| 1930 | * on navigations
|
| 1931 | *
|
| 1932 | * <!--
|
| 1933 | * Not marked `@public` because we only export as UNSAFE_ and therefore we don't
|
| 1934 | * maintain an .md file for this hook
|
| 1935 | * -->
|
| 1936 | *
|
| 1937 | * @name UNSAFE_useScrollRestoration
|
| 1938 | * @category Hooks
|
| 1939 | * @mode framework
|
| 1940 | * @mode data
|
| 1941 | * @param options Options
|
| 1942 | * @param options.getKey A function that returns a key to use for scroll restoration.
|
| 1943 | * This is useful for custom scroll restoration logic, such as using only the pathname
|
| 1944 | * so that subsequent navigations to prior paths will restore the scroll. Defaults
|
| 1945 | * to `location.key`.
|
| 1946 | * @param options.storageKey The key to use for storing scroll positions in
|
| 1947 | * `sessionStorage`. Defaults to `"react-router-scroll-positions"`.
|
| 1948 | * @returns {void}
|
| 1949 | */
|
| 1950 | declare function useScrollRestoration({
|
| 1951 | getKey,
|
| 1952 | storageKey
|
| 1953 | }?: {
|
| 1954 | getKey?: GetScrollRestorationKeyFunction;
|
| 1955 | storageKey?: string;
|
| 1956 | }): void;
|
| 1957 | |
| 1958 | * Set up a callback to be fired on [Window's `beforeunload` event](https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event).
|
| 1959 | *
|
| 1960 | * @public
|
| 1961 | * @category Hooks
|
| 1962 | * @param callback The callback to be called when the [`beforeunload` event](https:
|
| 1963 | * is fired.
|
| 1964 | * @param options Options
|
| 1965 | * @param options.capture If `true`, the event will be captured during the capture
|
| 1966 | * phase. Defaults to `false`.
|
| 1967 | * @returns {void}
|
| 1968 | */
|
| 1969 | declare function useBeforeUnload(callback: (event: BeforeUnloadEvent) => any, options?: {
|
| 1970 | capture?: boolean;
|
| 1971 | }): void;
|
| 1972 | |
| 1973 | * Wrapper around {@link useBlocker} to show a [`window.confirm`](https:
|
| 1974 | * prompt to users instead of building a custom UI with {@link useBlocker}.
|
| 1975 | *
|
| 1976 | * The `unstable_` flag will not be removed because this technique has a lot of
|
| 1977 | * rough edges and behaves very differently (and incorrectly sometimes) across
|
| 1978 | * browsers if users click addition back/forward navigations while the
|
| 1979 | * confirmation is open. Use at your own risk.
|
| 1980 | *
|
| 1981 | * @example
|
| 1982 | * function ImportantForm() {
|
| 1983 | * let [value, setValue] = React.useState("");
|
| 1984 | *
|
| 1985 | *
|
| 1986 | * unstable_usePrompt({
|
| 1987 | * message: "Are you sure?",
|
| 1988 | * when: ({ currentLocation, nextLocation }) =>
|
| 1989 | * value !== "" &&
|
| 1990 | * currentLocation.pathname !== nextLocation.pathname,
|
| 1991 | * });
|
| 1992 | *
|
| 1993 | * return (
|
| 1994 | * <Form method="post">
|
| 1995 | * <label>
|
| 1996 | * Enter some important data:
|
| 1997 | * <input
|
| 1998 | * name="data"
|
| 1999 | * value={value}
|
| 2000 | * onChange={(e) => setValue(e.target.value)}
|
| 2001 | * />
|
| 2002 | * </label>
|
| 2003 | * <button type="submit">Save</button>
|
| 2004 | * </Form>
|
| 2005 | * );
|
| 2006 | * }
|
| 2007 | *
|
| 2008 | * @name unstable_usePrompt
|
| 2009 | * @public
|
| 2010 | * @category Hooks
|
| 2011 | * @mode framework
|
| 2012 | * @mode data
|
| 2013 | * @param options Options
|
| 2014 | * @param options.message The message to show in the confirmation dialog.
|
| 2015 | * @param options.when A boolean or a function that returns a boolean indicating
|
| 2016 | * whether to block the navigation. If a function is provided, it will receive an
|
| 2017 | * object with `currentLocation` and `nextLocation` properties.
|
| 2018 | * @returns {void}
|
| 2019 | */
|
| 2020 | declare function usePrompt({
|
| 2021 | when,
|
| 2022 | message
|
| 2023 | }: {
|
| 2024 | when: boolean | BlockerFunction;
|
| 2025 | message: string;
|
| 2026 | }): void;
|
| 2027 | |
| 2028 | * This hook returns `true` when there is an active [View Transition](https:
|
| 2029 | * and the specified location matches either side of the navigation (the URL you are
|
| 2030 | * navigating **to** or the URL you are navigating **from**). This can be used to apply finer-grained styles to
|
| 2031 | * elements to further customize the view transition. This requires that view
|
| 2032 | * transitions have been enabled for the given navigation via {@link LinkProps.viewTransition}
|
| 2033 | * (or the `Form`, `submit`, or `navigate` call)
|
| 2034 | *
|
| 2035 | * @public
|
| 2036 | * @category Hooks
|
| 2037 | * @mode framework
|
| 2038 | * @mode data
|
| 2039 | * @param to The {@link To} location to compare against the active transition's current
|
| 2040 | * and next URLs.
|
| 2041 | * @param options Options
|
| 2042 | * @param options.relative The relative routing type to use when resolving the
|
| 2043 | * `to` location, defaults to `"route"`. See {@link RelativeRoutingType} for
|
| 2044 | * more details.
|
| 2045 | * @returns `true` if there is an active [View Transition](https:
|
| 2046 | * and the resolved path matches the transition's destination or source pathname, otherwise `false`.
|
| 2047 | */
|
| 2048 | declare function useViewTransitionState(to: To, {
|
| 2049 | relative
|
| 2050 | }?: {
|
| 2051 | relative?: RelativeRoutingType;
|
| 2052 | }): boolean;
|
| 2053 |
|
| 2054 | export { BrowserRouter, BrowserRouterProps, DOMRouterOpts, FetcherFormProps, FetcherSubmitFunction, FetcherWithComponents, Form, FormProps, HashRouter, HashRouterProps, HistoryRouter, HistoryRouterProps, Link, LinkProps, NavLink, NavLinkProps, NavLinkRenderProps, ScrollRestoration, ScrollRestorationProps, SetURLSearchParams, SubmitFunction, createBrowserRouter, createHashRouter, useBeforeUnload, useFetcher, useFetchers, useFormAction, useLinkClickHandler, usePrompt, useScrollRestoration, useSearchParams, useSubmit, useViewTransitionState }; |
| \ | No newline at end of file |