react-router 7.16.0 → 8.0.0-pre.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +0 -1
- package/dist/development/dom-export.d.ts +6 -172
- package/dist/development/dom-export.js +12 -1007
- package/dist/development/index-react-server-client.d.ts +7 -4
- package/dist/development/index-react-server-client.js +8 -52
- package/dist/development/index-react-server.d.ts +1645 -1635
- package/dist/development/index-react-server.js +2880 -3642
- package/dist/development/index.d.ts +43 -1475
- package/dist/development/index.js +37 -2621
- package/dist/development/lib/actions.js +62 -0
- package/dist/development/lib/components.d.ts +1022 -0
- package/dist/development/lib/components.js +835 -0
- package/dist/development/lib/context.d.ts +83 -0
- package/dist/development/lib/context.js +41 -0
- package/dist/development/lib/dom/dom.d.ts +119 -0
- package/dist/development/lib/dom/dom.js +143 -0
- package/dist/development/lib/dom/lib.d.ts +2042 -0
- package/dist/development/lib/dom/lib.js +1259 -0
- package/dist/development/lib/dom/server.d.ts +138 -0
- package/dist/development/lib/dom/server.js +301 -0
- package/dist/development/lib/dom/ssr/components.d.ts +196 -0
- package/dist/development/lib/dom/ssr/components.js +579 -0
- package/dist/development/lib/dom/ssr/data.js +29 -0
- package/dist/development/lib/dom/ssr/entry.d.ts +59 -0
- package/dist/development/lib/dom/ssr/errorBoundaries.d.ts +27 -0
- package/dist/development/lib/dom/ssr/errorBoundaries.js +83 -0
- package/dist/development/lib/dom/ssr/errors.d.ts +7 -0
- package/dist/development/lib/dom/ssr/errors.js +36 -0
- package/dist/development/lib/dom/ssr/fallback.js +28 -0
- package/dist/development/lib/dom/ssr/fog-of-war.d.ts +12 -0
- package/dist/development/lib/dom/ssr/fog-of-war.js +170 -0
- package/dist/development/lib/dom/ssr/hydration.d.ts +32 -0
- package/dist/development/lib/dom/ssr/hydration.js +29 -0
- package/dist/development/lib/dom/ssr/invariant.js +16 -0
- package/dist/development/lib/dom/ssr/links.js +170 -0
- package/dist/development/lib/dom/ssr/markup.js +24 -0
- package/dist/development/lib/dom/ssr/routeModules.d.ts +206 -0
- package/dist/development/lib/dom/ssr/routeModules.js +31 -0
- package/dist/development/lib/dom/ssr/routes-test-stub.d.ts +62 -0
- package/dist/development/lib/dom/ssr/routes-test-stub.js +108 -0
- package/dist/development/lib/dom/ssr/routes.d.ts +33 -0
- package/dist/development/lib/dom/ssr/routes.js +303 -0
- package/dist/development/lib/dom/ssr/server.d.ts +45 -0
- package/dist/development/lib/dom/ssr/server.js +68 -0
- package/dist/development/lib/dom/ssr/single-fetch.d.ts +14 -0
- package/dist/development/lib/dom/ssr/single-fetch.js +346 -0
- package/dist/development/lib/dom-export/dom-router-provider.d.ts +9 -0
- package/dist/development/lib/dom-export/dom-router-provider.js +22 -0
- package/dist/development/lib/dom-export/hydrated-router.d.ts +125 -0
- package/dist/development/lib/dom-export/hydrated-router.js +153 -0
- package/dist/development/lib/errors.js +29 -0
- package/dist/development/lib/hooks.d.ts +947 -0
- package/dist/development/lib/hooks.js +1386 -0
- package/dist/development/lib/href.d.ts +20 -0
- package/dist/development/lib/href.js +50 -0
- package/dist/development/lib/router/history.d.ts +258 -0
- package/dist/development/lib/router/history.js +371 -0
- package/dist/development/lib/router/instrumentation.d.ts +86 -0
- package/dist/development/lib/router/instrumentation.js +213 -0
- package/dist/development/lib/router/links.d.ts +113 -0
- package/dist/development/lib/router/router.d.ts +663 -0
- package/dist/development/lib/router/router.js +2981 -0
- package/dist/development/lib/router/utils.d.ts +942 -0
- package/dist/development/lib/router/utils.js +791 -0
- package/dist/development/lib/rsc/browser.d.ts +137 -0
- package/dist/development/lib/rsc/browser.js +599 -0
- package/dist/development/lib/rsc/errorBoundaries.d.ts +11 -0
- package/dist/development/lib/rsc/errorBoundaries.js +90 -0
- package/dist/development/lib/rsc/html-stream/browser.d.ts +48 -0
- package/dist/development/lib/rsc/html-stream/browser.js +74 -0
- package/dist/development/lib/rsc/html-stream/server.js +78 -0
- package/dist/development/lib/rsc/route-modules.js +27 -0
- package/dist/development/lib/rsc/server.rsc.d.ts +219 -0
- package/dist/development/lib/rsc/server.ssr.d.ts +129 -0
- package/dist/development/lib/rsc/server.ssr.js +388 -0
- package/dist/development/lib/server-runtime/build.d.ts +66 -0
- package/dist/development/lib/server-runtime/cookies.d.ts +66 -0
- package/dist/development/lib/server-runtime/cookies.js +139 -0
- package/dist/development/lib/server-runtime/crypto.js +43 -0
- package/dist/development/lib/server-runtime/data.d.ts +13 -0
- package/dist/development/lib/server-runtime/data.js +25 -0
- package/dist/development/lib/server-runtime/dev.d.ts +9 -0
- package/dist/development/lib/server-runtime/dev.js +26 -0
- package/dist/development/lib/server-runtime/entry.js +20 -0
- package/dist/development/lib/server-runtime/errors.js +95 -0
- package/dist/development/lib/server-runtime/headers.js +73 -0
- package/dist/development/lib/server-runtime/invariant.js +19 -0
- package/dist/development/lib/server-runtime/mode.d.ts +12 -0
- package/dist/development/lib/server-runtime/mode.js +25 -0
- package/dist/development/lib/server-runtime/routeMatching.js +28 -0
- package/dist/development/lib/server-runtime/routes.d.ts +13 -0
- package/dist/development/lib/server-runtime/routes.js +74 -0
- package/dist/development/lib/server-runtime/server.d.ts +10 -0
- package/dist/development/lib/server-runtime/server.js +351 -0
- package/dist/development/lib/server-runtime/serverHandoff.js +17 -0
- package/dist/development/lib/server-runtime/sessions/cookieStorage.d.ts +25 -0
- package/dist/development/lib/server-runtime/sessions/cookieStorage.js +45 -0
- package/dist/development/lib/server-runtime/sessions/memoryStorage.d.ts +23 -0
- package/dist/development/lib/server-runtime/sessions/memoryStorage.js +52 -0
- package/dist/development/lib/server-runtime/sessions.d.ts +145 -0
- package/dist/development/lib/server-runtime/sessions.js +98 -0
- package/dist/development/lib/server-runtime/single-fetch.d.ts +7 -0
- package/dist/development/lib/server-runtime/single-fetch.js +215 -0
- package/dist/development/lib/server-runtime/urls.js +31 -0
- package/dist/development/lib/server-runtime/warnings.js +20 -0
- package/dist/development/lib/types/future.d.ts +9 -0
- package/dist/development/lib/types/internal.d.ts +26 -177
- package/dist/development/lib/types/internal.js +3 -2
- package/dist/{production/register-CNAx3TXj.d.ts → development/lib/types/register.d.ts} +9 -15
- package/dist/development/lib/types/route-data.d.ts +113 -0
- package/dist/development/lib/types/route-module-annotations.d.ts +149 -0
- package/dist/development/lib/types/route-module.d.ts +19 -0
- package/dist/development/lib/types/serializes-to.d.ts +13 -0
- package/dist/development/lib/types/utils.d.ts +11 -0
- package/dist/development/vendor/turbo-stream-v2/flatten.js +159 -0
- package/dist/development/vendor/turbo-stream-v2/turbo-stream.js +178 -0
- package/dist/development/vendor/turbo-stream-v2/unflatten.js +198 -0
- package/dist/development/vendor/turbo-stream-v2/utils.js +47 -0
- package/dist/production/dom-export.d.ts +6 -172
- package/dist/production/dom-export.js +12 -1007
- package/dist/production/index-react-server-client.d.ts +7 -4
- package/dist/production/index-react-server-client.js +8 -52
- package/dist/production/index-react-server.d.ts +1645 -1635
- package/dist/production/index-react-server.js +2871 -3642
- package/dist/production/index.d.ts +43 -1475
- package/dist/production/index.js +37 -2621
- package/dist/production/lib/actions.js +62 -0
- package/dist/production/lib/components.d.ts +1022 -0
- package/dist/production/lib/components.js +835 -0
- package/dist/production/lib/context.d.ts +83 -0
- package/dist/production/lib/context.js +41 -0
- package/dist/production/lib/dom/dom.d.ts +119 -0
- package/dist/production/lib/dom/dom.js +143 -0
- package/dist/production/lib/dom/lib.d.ts +2042 -0
- package/dist/production/lib/dom/lib.js +1259 -0
- package/dist/production/lib/dom/server.d.ts +138 -0
- package/dist/production/lib/dom/server.js +301 -0
- package/dist/production/lib/dom/ssr/components.d.ts +196 -0
- package/dist/production/lib/dom/ssr/components.js +579 -0
- package/dist/production/lib/dom/ssr/data.js +29 -0
- package/dist/production/lib/dom/ssr/entry.d.ts +59 -0
- package/dist/production/lib/dom/ssr/errorBoundaries.d.ts +27 -0
- package/dist/production/lib/dom/ssr/errorBoundaries.js +83 -0
- package/dist/production/lib/dom/ssr/errors.d.ts +7 -0
- package/dist/production/lib/dom/ssr/errors.js +36 -0
- package/dist/production/lib/dom/ssr/fallback.js +21 -0
- package/dist/production/lib/dom/ssr/fog-of-war.d.ts +12 -0
- package/dist/production/lib/dom/ssr/fog-of-war.js +170 -0
- package/dist/production/lib/dom/ssr/hydration.d.ts +32 -0
- package/dist/production/lib/dom/ssr/hydration.js +29 -0
- package/dist/production/lib/dom/ssr/invariant.js +16 -0
- package/dist/production/lib/dom/ssr/links.js +170 -0
- package/dist/production/lib/dom/ssr/markup.js +24 -0
- package/dist/production/lib/dom/ssr/routeModules.d.ts +206 -0
- package/dist/production/lib/dom/ssr/routeModules.js +31 -0
- package/dist/production/lib/dom/ssr/routes-test-stub.d.ts +62 -0
- package/dist/production/lib/dom/ssr/routes-test-stub.js +108 -0
- package/dist/production/lib/dom/ssr/routes.d.ts +33 -0
- package/dist/production/lib/dom/ssr/routes.js +303 -0
- package/dist/production/lib/dom/ssr/server.d.ts +45 -0
- package/dist/production/lib/dom/ssr/server.js +68 -0
- package/dist/production/lib/dom/ssr/single-fetch.d.ts +14 -0
- package/dist/production/lib/dom/ssr/single-fetch.js +346 -0
- package/dist/production/lib/dom-export/dom-router-provider.d.ts +9 -0
- package/dist/production/lib/dom-export/dom-router-provider.js +22 -0
- package/dist/production/lib/dom-export/hydrated-router.d.ts +125 -0
- package/dist/production/lib/dom-export/hydrated-router.js +153 -0
- package/dist/production/lib/errors.js +29 -0
- package/dist/production/lib/hooks.d.ts +947 -0
- package/dist/production/lib/hooks.js +1371 -0
- package/dist/production/lib/href.d.ts +20 -0
- package/dist/production/lib/href.js +50 -0
- package/dist/production/lib/router/history.d.ts +258 -0
- package/dist/production/lib/router/history.js +371 -0
- package/dist/production/lib/router/instrumentation.d.ts +86 -0
- package/dist/production/lib/router/instrumentation.js +213 -0
- package/dist/production/lib/router/links.d.ts +113 -0
- package/dist/production/lib/router/router.d.ts +663 -0
- package/dist/production/lib/router/router.js +2981 -0
- package/dist/production/lib/router/utils.d.ts +942 -0
- package/dist/production/lib/router/utils.js +782 -0
- package/dist/production/lib/rsc/browser.d.ts +137 -0
- package/dist/production/lib/rsc/browser.js +599 -0
- package/dist/production/lib/rsc/errorBoundaries.d.ts +11 -0
- package/dist/production/lib/rsc/errorBoundaries.js +90 -0
- package/dist/production/lib/rsc/html-stream/browser.d.ts +48 -0
- package/dist/production/lib/rsc/html-stream/browser.js +74 -0
- package/dist/production/lib/rsc/html-stream/server.js +78 -0
- package/dist/production/lib/rsc/route-modules.js +27 -0
- package/dist/production/lib/rsc/server.rsc.d.ts +219 -0
- package/dist/production/lib/rsc/server.ssr.d.ts +129 -0
- package/dist/production/lib/rsc/server.ssr.js +388 -0
- package/dist/production/lib/server-runtime/build.d.ts +66 -0
- package/dist/production/lib/server-runtime/cookies.d.ts +66 -0
- package/dist/production/lib/server-runtime/cookies.js +139 -0
- package/dist/production/lib/server-runtime/crypto.js +43 -0
- package/dist/production/lib/server-runtime/data.d.ts +13 -0
- package/dist/production/lib/server-runtime/data.js +25 -0
- package/dist/production/lib/server-runtime/dev.d.ts +9 -0
- package/dist/production/lib/server-runtime/dev.js +26 -0
- package/dist/production/lib/server-runtime/entry.js +20 -0
- package/dist/production/lib/server-runtime/errors.js +95 -0
- package/dist/production/lib/server-runtime/headers.js +73 -0
- package/dist/production/lib/server-runtime/invariant.js +19 -0
- package/dist/production/lib/server-runtime/mode.d.ts +12 -0
- package/dist/production/lib/server-runtime/mode.js +25 -0
- package/dist/production/lib/server-runtime/routeMatching.js +28 -0
- package/dist/production/lib/server-runtime/routes.d.ts +13 -0
- package/dist/production/lib/server-runtime/routes.js +74 -0
- package/dist/production/lib/server-runtime/server.d.ts +10 -0
- package/dist/production/lib/server-runtime/server.js +351 -0
- package/dist/production/lib/server-runtime/serverHandoff.js +17 -0
- package/dist/production/lib/server-runtime/sessions/cookieStorage.d.ts +25 -0
- package/dist/production/lib/server-runtime/sessions/cookieStorage.js +45 -0
- package/dist/production/lib/server-runtime/sessions/memoryStorage.d.ts +23 -0
- package/dist/production/lib/server-runtime/sessions/memoryStorage.js +52 -0
- package/dist/production/lib/server-runtime/sessions.d.ts +145 -0
- package/dist/production/lib/server-runtime/sessions.js +98 -0
- package/dist/production/lib/server-runtime/single-fetch.d.ts +7 -0
- package/dist/production/lib/server-runtime/single-fetch.js +215 -0
- package/dist/production/lib/server-runtime/urls.js +31 -0
- package/dist/production/lib/server-runtime/warnings.js +20 -0
- package/dist/production/lib/types/future.d.ts +9 -0
- package/dist/production/lib/types/internal.d.ts +26 -177
- package/dist/production/lib/types/internal.js +3 -2
- package/dist/{development/register-CNAx3TXj.d.ts → production/lib/types/register.d.ts} +9 -15
- package/dist/production/lib/types/route-data.d.ts +113 -0
- package/dist/production/lib/types/route-module-annotations.d.ts +149 -0
- package/dist/production/lib/types/route-module.d.ts +19 -0
- package/dist/production/lib/types/serializes-to.d.ts +13 -0
- package/dist/production/lib/types/utils.d.ts +11 -0
- package/dist/production/vendor/turbo-stream-v2/flatten.js +159 -0
- package/dist/production/vendor/turbo-stream-v2/turbo-stream.js +178 -0
- package/dist/production/vendor/turbo-stream-v2/unflatten.js +198 -0
- package/dist/production/vendor/turbo-stream-v2/utils.js +47 -0
- package/docs/explanation/backend-for-frontend.md +50 -0
- package/docs/explanation/code-splitting.md +77 -0
- package/docs/explanation/concurrency.md +135 -0
- package/docs/explanation/form-vs-fetcher.md +292 -0
- package/docs/explanation/hot-module-replacement.md +137 -0
- package/docs/explanation/hydration.md +14 -0
- package/docs/explanation/index-query-param.md +86 -0
- package/docs/explanation/index.md +4 -0
- package/docs/explanation/lazy-route-discovery.md +78 -0
- package/docs/explanation/location.md +6 -0
- package/docs/explanation/progressive-enhancement.md +150 -0
- package/docs/explanation/race-conditions.md +88 -0
- package/docs/explanation/react-transitions.md +160 -0
- package/docs/explanation/route-matching.md +7 -0
- package/docs/explanation/server-client-execution.md +4 -0
- package/docs/explanation/sessions-and-cookies.md +465 -0
- package/docs/explanation/special-files.md +16 -0
- package/docs/explanation/state-management.md +524 -0
- package/docs/explanation/styling.md +87 -0
- package/docs/explanation/type-safety.md +82 -0
- package/docs/how-to/accessibility.md +44 -0
- package/docs/how-to/client-data.md +199 -0
- package/docs/how-to/data-strategy.md +317 -0
- package/docs/how-to/error-boundary.md +231 -0
- package/docs/how-to/error-reporting.md +134 -0
- package/docs/how-to/fetchers.md +307 -0
- package/docs/how-to/file-route-conventions.md +410 -0
- package/docs/how-to/file-uploads.md +217 -0
- package/docs/how-to/form-validation.md +120 -0
- package/docs/how-to/headers.md +164 -0
- package/docs/how-to/index.md +4 -0
- package/docs/how-to/instrumentation.md +556 -0
- package/docs/how-to/meta.md +40 -0
- package/docs/how-to/middleware.md +728 -0
- package/docs/how-to/navigation-blocking.md +233 -0
- package/docs/how-to/optimize-revalidation.md +12 -0
- package/docs/how-to/pre-rendering.md +225 -0
- package/docs/how-to/presets.md +103 -0
- package/docs/how-to/react-server-components.md +899 -0
- package/docs/how-to/resource-routes.md +126 -0
- package/docs/how-to/route-module-type-safety.md +100 -0
- package/docs/how-to/search-params.md +4 -0
- package/docs/how-to/security.md +30 -0
- package/docs/how-to/server-bundles.md +66 -0
- package/docs/how-to/spa.md +120 -0
- package/docs/how-to/status.md +63 -0
- package/docs/how-to/suspense.md +132 -0
- package/docs/how-to/using-handle.md +117 -0
- package/docs/how-to/view-transitions.md +237 -0
- package/docs/how-to/webhook.md +50 -0
- package/docs/index.md +39 -0
- package/docs/start/data/actions.md +138 -0
- package/docs/start/data/custom.md +198 -0
- package/docs/start/data/data-loading.md +44 -0
- package/docs/start/data/index.md +4 -0
- package/docs/start/data/installation.md +52 -0
- package/docs/start/data/navigating.md +12 -0
- package/docs/start/data/pending-ui.md +12 -0
- package/docs/start/data/route-object.md +248 -0
- package/docs/start/data/routing.md +281 -0
- package/docs/start/data/testing.md +8 -0
- package/docs/start/declarative/index.md +4 -0
- package/docs/start/declarative/installation.md +43 -0
- package/docs/start/declarative/navigating.md +133 -0
- package/docs/start/declarative/routing.md +237 -0
- package/docs/start/declarative/url-values.md +65 -0
- package/docs/start/framework/actions.md +175 -0
- package/docs/start/framework/data-loading.md +201 -0
- package/docs/start/framework/deploying.md +96 -0
- package/docs/start/framework/index.md +4 -0
- package/docs/start/framework/installation.md +42 -0
- package/docs/start/framework/navigating.md +182 -0
- package/docs/start/framework/pending-ui.md +142 -0
- package/docs/start/framework/rendering.md +59 -0
- package/docs/start/framework/route-module.md +527 -0
- package/docs/start/framework/routing.md +362 -0
- package/docs/start/framework/testing.md +133 -0
- package/docs/start/index.md +4 -0
- package/docs/start/modes.md +201 -0
- package/docs/upgrading/component-routes.md +363 -0
- package/docs/upgrading/future.md +31 -0
- package/docs/upgrading/index.md +4 -0
- package/docs/upgrading/remix.md +403 -0
- package/docs/upgrading/router-provider.md +442 -0
- package/docs/upgrading/v6.md +379 -0
- package/package.json +44 -87
- package/dist/development/browser-D3uq9sI1.d.ts +0 -318
- package/dist/development/browser-nIQ4Nsyi.d.mts +0 -318
- package/dist/development/chunk-IBI7OMNB.js +0 -1363
- package/dist/development/chunk-QUQL4437.mjs +0 -11529
- package/dist/development/chunk-S54KXAEJ.mjs +0 -2585
- package/dist/development/chunk-SRID2YZ2.js +0 -10229
- package/dist/development/chunk-XEJDWL2B.js +0 -188
- package/dist/development/context-m8rizgnE.d.mts +0 -1771
- package/dist/development/data-D4xhSy90.d.ts +0 -1732
- package/dist/development/data-U8FS-wNn.d.mts +0 -1732
- package/dist/development/dom-export.d.mts +0 -172
- package/dist/development/dom-export.mjs +0 -1008
- package/dist/development/index-react-server-client-BLiUx67a.d.ts +0 -3655
- package/dist/development/index-react-server-client-CdKROblb.d.mts +0 -2600
- package/dist/development/index-react-server-client.d.mts +0 -4
- package/dist/development/index-react-server-client.mjs +0 -59
- package/dist/development/index-react-server.d.mts +0 -2703
- package/dist/development/index-react-server.mjs +0 -3780
- package/dist/development/index.d.mts +0 -1478
- package/dist/development/index.mjs +0 -277
- package/dist/development/instrumentation-1q4YhLGP.d.ts +0 -715
- package/dist/development/lib/types/internal.d.mts +0 -184
- package/dist/development/lib/types/internal.mjs +0 -10
- package/dist/development/register-CqK96Zfk.d.mts +0 -30
- package/dist/production/browser-D3uq9sI1.d.ts +0 -318
- package/dist/production/browser-nIQ4Nsyi.d.mts +0 -318
- package/dist/production/chunk-EAQNHM3N.js +0 -188
- package/dist/production/chunk-NALGHHKE.mjs +0 -2585
- package/dist/production/chunk-Q65P7S7Y.mjs +0 -11529
- package/dist/production/chunk-SKEDDLRM.js +0 -1363
- package/dist/production/chunk-Y7DNFQZP.js +0 -10229
- package/dist/production/context-m8rizgnE.d.mts +0 -1771
- package/dist/production/data-D4xhSy90.d.ts +0 -1732
- package/dist/production/data-U8FS-wNn.d.mts +0 -1732
- package/dist/production/dom-export.d.mts +0 -172
- package/dist/production/dom-export.mjs +0 -1008
- package/dist/production/index-react-server-client-BLiUx67a.d.ts +0 -3655
- package/dist/production/index-react-server-client-CdKROblb.d.mts +0 -2600
- package/dist/production/index-react-server-client.d.mts +0 -4
- package/dist/production/index-react-server-client.mjs +0 -59
- package/dist/production/index-react-server.d.mts +0 -2703
- package/dist/production/index-react-server.mjs +0 -3780
- package/dist/production/index.d.mts +0 -1478
- package/dist/production/index.mjs +0 -277
- package/dist/production/instrumentation-1q4YhLGP.d.ts +0 -715
- package/dist/production/lib/types/internal.d.mts +0 -184
- package/dist/production/lib/types/internal.mjs +0 -10
- package/dist/production/register-CqK96Zfk.d.mts +0 -30
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Form vs. fetcher
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
# Form vs. fetcher
|
|
6
|
+
|
|
7
|
+
[MODES: framework, data]
|
|
8
|
+
|
|
9
|
+
## Overview
|
|
10
|
+
|
|
11
|
+
Developing in React Router offers a rich set of tools that can sometimes overlap in functionality, creating a sense of ambiguity for newcomers. The key to effective development in React Router is understanding the nuances and appropriate use cases for each tool. This document seeks to provide clarity on when and why to use specific APIs.
|
|
12
|
+
|
|
13
|
+
## APIs in Focus
|
|
14
|
+
|
|
15
|
+
- [`<Form>`][form-component]
|
|
16
|
+
- [`useFetcher`][use-fetcher]
|
|
17
|
+
- [`useNavigation`][use-navigation]
|
|
18
|
+
|
|
19
|
+
Understanding the distinctions and intersections of these APIs is vital for efficient and effective React Router development.
|
|
20
|
+
|
|
21
|
+
## URL Considerations
|
|
22
|
+
|
|
23
|
+
The primary criterion when choosing among these tools is whether you want the URL to change or not:
|
|
24
|
+
|
|
25
|
+
- **URL Change Desired**: When navigating or transitioning between pages, or after certain actions like creating or deleting records. This ensures that the user's browser history accurately reflects their journey through your application.
|
|
26
|
+
- **Expected Behavior**: In many cases, when users hit the back button, they should be taken to the previous page. Other times the history entry may be replaced but the URL change is important nonetheless.
|
|
27
|
+
|
|
28
|
+
- **No URL Change Desired**: For actions that don't significantly change the context or primary content of the current view. This might include updating individual fields or minor data manipulations that don't warrant a new URL or page reload. This also applies to loading data with fetchers for things like popovers, combo boxes, etc.
|
|
29
|
+
|
|
30
|
+
### When the URL Should Change
|
|
31
|
+
|
|
32
|
+
These actions typically reflect significant changes to the user's context or state:
|
|
33
|
+
|
|
34
|
+
- **Creating a New Record**: After creating a new record, it's common to redirect users to a page dedicated to that new record, where they can view or further modify it.
|
|
35
|
+
|
|
36
|
+
- **Deleting a Record**: If a user is on a page dedicated to a specific record and decides to delete it, the logical next step is to redirect them to a general page, such as a list of all records.
|
|
37
|
+
|
|
38
|
+
For these cases, developers should consider using a combination of [`<Form>`][form-component] and [`useNavigation`][use-navigation]. These tools can be coordinated to handle form submission, invoke specific actions, retrieve action-related data through component props, and manage navigation respectively.
|
|
39
|
+
|
|
40
|
+
### When the URL Shouldn't Change
|
|
41
|
+
|
|
42
|
+
These actions are generally more subtle and don't require a context switch for the user:
|
|
43
|
+
|
|
44
|
+
- **Updating a Single Field**: Maybe a user wants to change the name of an item in a list or update a specific property of a record. This action is minor and doesn't necessitate a new page or URL.
|
|
45
|
+
|
|
46
|
+
- **Deleting a Record from a List**: In a list view, if a user deletes an item, they likely expect to remain on the list view, with that item no longer in the list.
|
|
47
|
+
|
|
48
|
+
- **Creating a Record in a List View**: When adding a new item to a list, it often makes sense for the user to remain in that context, seeing their new item added to the list without a full page transition.
|
|
49
|
+
|
|
50
|
+
- **Loading Data for a Popover or Combobox**: When loading data for a popover or combobox, the user's context remains unchanged. The data is loaded in the background and displayed in a small, self-contained UI element.
|
|
51
|
+
|
|
52
|
+
For such actions, [`useFetcher`][use-fetcher] is the go-to API. It's versatile, combining functionalities of these APIs, and is perfectly suited for tasks where the URL should remain unchanged.
|
|
53
|
+
|
|
54
|
+
## API Comparison
|
|
55
|
+
|
|
56
|
+
As you can see, the two sets of APIs have a lot of similarities:
|
|
57
|
+
|
|
58
|
+
| Navigation/URL API | Fetcher API |
|
|
59
|
+
| ----------------------------- | -------------------- |
|
|
60
|
+
| `<Form>` | `<fetcher.Form>` |
|
|
61
|
+
| `actionData` (component prop) | `fetcher.data` |
|
|
62
|
+
| `navigation.state` | `fetcher.state` |
|
|
63
|
+
| `navigation.formAction` | `fetcher.formAction` |
|
|
64
|
+
| `navigation.formData` | `fetcher.formData` |
|
|
65
|
+
|
|
66
|
+
## Examples
|
|
67
|
+
|
|
68
|
+
### Creating a New Record
|
|
69
|
+
|
|
70
|
+
```tsx filename=app/pages/new-recipe.tsx lines=[16,23-24,29]
|
|
71
|
+
import {
|
|
72
|
+
Form,
|
|
73
|
+
redirect,
|
|
74
|
+
useNavigation,
|
|
75
|
+
} from "react-router";
|
|
76
|
+
import type { Route } from "./+types/new-recipe";
|
|
77
|
+
|
|
78
|
+
export async function action({
|
|
79
|
+
request,
|
|
80
|
+
}: Route.ActionArgs) {
|
|
81
|
+
const formData = await request.formData();
|
|
82
|
+
const errors = await validateRecipeFormData(formData);
|
|
83
|
+
if (errors) {
|
|
84
|
+
return { errors };
|
|
85
|
+
}
|
|
86
|
+
const recipe = await db.recipes.create(formData);
|
|
87
|
+
return redirect(`/recipes/${recipe.id}`);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function NewRecipe({
|
|
91
|
+
actionData,
|
|
92
|
+
}: Route.ComponentProps) {
|
|
93
|
+
const { errors } = actionData || {};
|
|
94
|
+
const navigation = useNavigation();
|
|
95
|
+
const isSubmitting =
|
|
96
|
+
navigation.formAction === "/recipes/new";
|
|
97
|
+
|
|
98
|
+
return (
|
|
99
|
+
<Form method="post">
|
|
100
|
+
<label>
|
|
101
|
+
Title: <input name="title" />
|
|
102
|
+
{errors?.title ? <span>{errors.title}</span> : null}
|
|
103
|
+
</label>
|
|
104
|
+
<label>
|
|
105
|
+
Ingredients: <textarea name="ingredients" />
|
|
106
|
+
{errors?.ingredients ? (
|
|
107
|
+
<span>{errors.ingredients}</span>
|
|
108
|
+
) : null}
|
|
109
|
+
</label>
|
|
110
|
+
<label>
|
|
111
|
+
Directions: <textarea name="directions" />
|
|
112
|
+
{errors?.directions ? (
|
|
113
|
+
<span>{errors.directions}</span>
|
|
114
|
+
) : null}
|
|
115
|
+
</label>
|
|
116
|
+
<button type="submit">
|
|
117
|
+
{isSubmitting ? "Saving..." : "Create Recipe"}
|
|
118
|
+
</button>
|
|
119
|
+
</Form>
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
The example leverages [`<Form>`][form-component], component props, and [`useNavigation`][use-navigation] to facilitate an intuitive record creation process.
|
|
125
|
+
|
|
126
|
+
Using `<Form>` ensures direct and logical navigation. After creating a record, the user is naturally guided to the new recipe's unique URL, reinforcing the outcome of their action.
|
|
127
|
+
|
|
128
|
+
The component props bridge server and client, providing immediate feedback on submission issues. This quick response enables users to rectify any errors without hindrance.
|
|
129
|
+
|
|
130
|
+
Lastly, `useNavigation` dynamically reflects the form's submission state. This subtle UI change, like toggling the button's label, assures users that their actions are being processed.
|
|
131
|
+
|
|
132
|
+
Combined, these APIs offer a balanced blend of structured navigation and feedback.
|
|
133
|
+
|
|
134
|
+
### Updating a Record
|
|
135
|
+
|
|
136
|
+
Now consider we're looking at a list of recipes that have delete buttons on each item. When a user clicks the delete button, we want to delete the recipe from the database and remove it from the list without navigating away from the list.
|
|
137
|
+
|
|
138
|
+
First, consider the basic route setup to get a list of recipes on the page:
|
|
139
|
+
|
|
140
|
+
```tsx filename=app/pages/recipes.tsx
|
|
141
|
+
import type { Route } from "./+types/recipes";
|
|
142
|
+
|
|
143
|
+
export async function loader({
|
|
144
|
+
request,
|
|
145
|
+
}: Route.LoaderArgs) {
|
|
146
|
+
return {
|
|
147
|
+
recipes: await db.recipes.findAll({ limit: 30 }),
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export default function Recipes({
|
|
152
|
+
loaderData,
|
|
153
|
+
}: Route.ComponentProps) {
|
|
154
|
+
const { recipes } = loaderData;
|
|
155
|
+
return (
|
|
156
|
+
<ul>
|
|
157
|
+
{recipes.map((recipe) => (
|
|
158
|
+
<RecipeListItem key={recipe.id} recipe={recipe} />
|
|
159
|
+
))}
|
|
160
|
+
</ul>
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
Now we'll look at the action that deletes a recipe and the component that renders each recipe in the list.
|
|
166
|
+
|
|
167
|
+
```tsx filename=app/pages/recipes.tsx lines=[10,21,27]
|
|
168
|
+
import { useFetcher } from "react-router";
|
|
169
|
+
import type { Recipe } from "./recipe.server";
|
|
170
|
+
import type { Route } from "./+types/recipes";
|
|
171
|
+
|
|
172
|
+
export async function action({
|
|
173
|
+
request,
|
|
174
|
+
}: Route.ActionArgs) {
|
|
175
|
+
const formData = await request.formData();
|
|
176
|
+
const id = formData.get("id");
|
|
177
|
+
await db.recipes.delete(id);
|
|
178
|
+
return { ok: true };
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export default function Recipes() {
|
|
182
|
+
return (
|
|
183
|
+
// ...
|
|
184
|
+
// doesn't matter, somewhere it's using <RecipeListItem />
|
|
185
|
+
)
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function RecipeListItem({ recipe }: { recipe: Recipe }) {
|
|
189
|
+
const fetcher = useFetcher();
|
|
190
|
+
const isDeleting = fetcher.state !== "idle";
|
|
191
|
+
|
|
192
|
+
return (
|
|
193
|
+
<li>
|
|
194
|
+
<h2>{recipe.title}</h2>
|
|
195
|
+
<fetcher.Form method="post">
|
|
196
|
+
<input type="hidden" name="id" value={recipe.id} />
|
|
197
|
+
<button disabled={isDeleting} type="submit">
|
|
198
|
+
{isDeleting ? "Deleting..." : "Delete"}
|
|
199
|
+
</button>
|
|
200
|
+
</fetcher.Form>
|
|
201
|
+
</li>
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
Using [`useFetcher`][use-fetcher] in this scenario works perfectly. Instead of navigating away or refreshing the entire page, we want in-place updates. When a user deletes a recipe, the `action` is called and the fetcher manages the corresponding state transitions.
|
|
207
|
+
|
|
208
|
+
The key advantage here is the maintenance of context. The user stays on the list when the deletion completes. The fetcher's state management capabilities are leveraged to give real-time feedback: it toggles between `"Deleting..."` and `"Delete"`, providing a clear indication of the ongoing process.
|
|
209
|
+
|
|
210
|
+
Furthermore, with each `fetcher` having the autonomy to manage its own state, operations on individual list items become independent, ensuring that actions on one item don't affect the others (though revalidation of the page data is a shared concern covered in [Network Concurrency Management][network-concurrency-management]).
|
|
211
|
+
|
|
212
|
+
In essence, `useFetcher` offers a seamless mechanism for actions that don't necessitate a change in the URL or navigation, enhancing the user experience by providing real-time feedback and context preservation.
|
|
213
|
+
|
|
214
|
+
### Mark Article as Read
|
|
215
|
+
|
|
216
|
+
Imagine you want to mark that an article has been read by the current user, after they've been on the page for a while and scrolled to the bottom. You could make a hook that looks something like this:
|
|
217
|
+
|
|
218
|
+
```tsx
|
|
219
|
+
import { useFetcher } from "react-router";
|
|
220
|
+
|
|
221
|
+
function useMarkAsRead({ articleId, userId }) {
|
|
222
|
+
const marker = useFetcher();
|
|
223
|
+
|
|
224
|
+
useSpentSomeTimeHereAndScrolledToTheBottom(() => {
|
|
225
|
+
marker.submit(
|
|
226
|
+
{ userId },
|
|
227
|
+
{
|
|
228
|
+
action: `/article/${articleId}/mark-as-read`,
|
|
229
|
+
method: "post",
|
|
230
|
+
},
|
|
231
|
+
);
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
### User Avatar Details Popup
|
|
237
|
+
|
|
238
|
+
Anytime you show the user avatar, you could put a hover effect that fetches data from a loader and displays it in a popup.
|
|
239
|
+
|
|
240
|
+
```tsx filename=app/pages/user-details.tsx
|
|
241
|
+
import { useState, useEffect } from "react";
|
|
242
|
+
import { useFetcher } from "react-router";
|
|
243
|
+
import type { Route } from "./+types/user-details";
|
|
244
|
+
|
|
245
|
+
export async function loader({ params }: Route.LoaderArgs) {
|
|
246
|
+
return await fakeDb.user.find({
|
|
247
|
+
where: { id: params.id },
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
type LoaderData = Route.ComponentProps["loaderData"];
|
|
252
|
+
|
|
253
|
+
function UserAvatar({ partialUser }) {
|
|
254
|
+
const userDetails = useFetcher<LoaderData>();
|
|
255
|
+
const [showDetails, setShowDetails] = useState(false);
|
|
256
|
+
|
|
257
|
+
useEffect(() => {
|
|
258
|
+
if (
|
|
259
|
+
showDetails &&
|
|
260
|
+
userDetails.state === "idle" &&
|
|
261
|
+
!userDetails.data
|
|
262
|
+
) {
|
|
263
|
+
userDetails.load(`/user-details/${partialUser.id}`);
|
|
264
|
+
}
|
|
265
|
+
}, [showDetails, userDetails, partialUser.id]);
|
|
266
|
+
|
|
267
|
+
return (
|
|
268
|
+
<div
|
|
269
|
+
onMouseEnter={() => setShowDetails(true)}
|
|
270
|
+
onMouseLeave={() => setShowDetails(false)}
|
|
271
|
+
>
|
|
272
|
+
<img src={partialUser.profileImageUrl} />
|
|
273
|
+
{showDetails ? (
|
|
274
|
+
userDetails.state === "idle" && userDetails.data ? (
|
|
275
|
+
<UserPopup user={userDetails.data} />
|
|
276
|
+
) : (
|
|
277
|
+
<UserPopupLoading />
|
|
278
|
+
)
|
|
279
|
+
) : null}
|
|
280
|
+
</div>
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
## Conclusion
|
|
286
|
+
|
|
287
|
+
React Router offers a range of tools to cater to varied developmental needs. While some functionalities might seem to overlap, each tool has been crafted with specific scenarios in mind. By understanding the intricacies and ideal applications of `<Form>`, `useFetcher`, and `useNavigation`, along with how data flows through component props, developers can create more intuitive, responsive, and user-friendly web applications.
|
|
288
|
+
|
|
289
|
+
[form-component]: ../api/components/Form
|
|
290
|
+
[use-fetcher]: ../api/hooks/useFetcher
|
|
291
|
+
[use-navigation]: ../api/hooks/useNavigation
|
|
292
|
+
[network-concurrency-management]: ./concurrency
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Hot Module Replacement
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
# Hot Module Replacement
|
|
6
|
+
|
|
7
|
+
[MODES: framework]
|
|
8
|
+
|
|
9
|
+
<br/>
|
|
10
|
+
<br/>
|
|
11
|
+
|
|
12
|
+
Hot Module Replacement is a technique for updating modules in your app without needing to reload the page.
|
|
13
|
+
It's a great developer experience, and React Router supports it when using Vite.
|
|
14
|
+
|
|
15
|
+
HMR does its best to preserve browser state across updates.
|
|
16
|
+
For example, let's say you have form within a modal and you fill out all the fields.
|
|
17
|
+
As soon as you save any changes to the code, traditional live reload would hard refresh the page causing all of those fields to be reset.
|
|
18
|
+
Every time you make a change, you'd have to open up the modal _again_ and fill out the form _again_.
|
|
19
|
+
|
|
20
|
+
But with HMR, all of that state is preserved _across updates_.
|
|
21
|
+
|
|
22
|
+
## React Fast Refresh
|
|
23
|
+
|
|
24
|
+
React already has mechanisms for updating the DOM via its [virtual DOM][virtual-dom] in response to user interactions like clicking a button.
|
|
25
|
+
Wouldn't it be great if React could handle updating the DOM in response to code changes too?
|
|
26
|
+
|
|
27
|
+
That's exactly what [React Fast Refresh][react-refresh] is all about!
|
|
28
|
+
Of course, React is all about components, not general JavaScript code, so React Fast Refresh only handles hot updates for exported React components.
|
|
29
|
+
|
|
30
|
+
But React Fast Refresh does have some limitations that you should be aware of.
|
|
31
|
+
|
|
32
|
+
### Class Component State
|
|
33
|
+
|
|
34
|
+
React Fast Refresh does not preserve state for class components.
|
|
35
|
+
This includes higher-order components that internally return classes:
|
|
36
|
+
|
|
37
|
+
```tsx
|
|
38
|
+
export class ComponentA extends Component {} // ❌
|
|
39
|
+
|
|
40
|
+
export const ComponentB = HOC(ComponentC); // ❌ Won't work if HOC returns a class component
|
|
41
|
+
|
|
42
|
+
export function ComponentD() {} // ✅
|
|
43
|
+
export const ComponentE = () => {}; // ✅
|
|
44
|
+
export default function ComponentF() {} // ✅
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Named Function Components
|
|
48
|
+
|
|
49
|
+
Function components must be named, not anonymous, for React Fast Refresh to track changes:
|
|
50
|
+
|
|
51
|
+
```tsx
|
|
52
|
+
export default () => {}; // ❌
|
|
53
|
+
export default function () {} // ❌
|
|
54
|
+
|
|
55
|
+
const ComponentA = () => {};
|
|
56
|
+
export default ComponentA; // ✅
|
|
57
|
+
|
|
58
|
+
export default function ComponentB() {} // ✅
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Supported Exports
|
|
62
|
+
|
|
63
|
+
React Fast Refresh can only handle component exports. While React Router manages [route exports like `action`, ` headers`, `links`, `loader`, and `meta`][route-module] for you, any user-defined exports will cause full reloads:
|
|
64
|
+
|
|
65
|
+
```tsx
|
|
66
|
+
// These exports are handled by the React Router Vite plugin
|
|
67
|
+
// to be HMR-compatible
|
|
68
|
+
export const meta = { title: "Home" }; // ✅
|
|
69
|
+
export const links = [
|
|
70
|
+
{ rel: "stylesheet", href: "style.css" },
|
|
71
|
+
]; // ✅
|
|
72
|
+
|
|
73
|
+
// These exports are removed by the React Router Vite plugin
|
|
74
|
+
// so they never affect HMR
|
|
75
|
+
export const headers = { "Cache-Control": "max-age=3600" }; // ✅
|
|
76
|
+
export const loader = async () => {}; // ✅
|
|
77
|
+
export const action = async () => {}; // ✅
|
|
78
|
+
|
|
79
|
+
// This is not a route module export, nor a component export,
|
|
80
|
+
// so it will cause a full reload for this route
|
|
81
|
+
export const myValue = "some value"; // ❌
|
|
82
|
+
|
|
83
|
+
export default function Route() {} // ✅
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
👆 Routes probably shouldn't be exporting random values like that anyway.
|
|
87
|
+
If you want to reuse values across routes, stick them in their own non-route module:
|
|
88
|
+
|
|
89
|
+
```ts filename=my-custom-value.ts
|
|
90
|
+
export const myValue = "some value";
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Changing Hooks
|
|
94
|
+
|
|
95
|
+
React Fast Refresh cannot track changes for a component when hooks are being added or removed from it, causing full reloads just for the next render. After the hooks have been updated, changes should result in hot updates again. For example, if you add a `useState` to your component, you may lose that component's local state for the next render.
|
|
96
|
+
|
|
97
|
+
Additionally, if you are destructuring a hook's return value, React Fast Refresh will not be able to preserve state for the component if the destructured key is removed or renamed.
|
|
98
|
+
For example:
|
|
99
|
+
|
|
100
|
+
```tsx
|
|
101
|
+
export default function Component({ loaderData }) {
|
|
102
|
+
const { pet } = useMyCustomHook();
|
|
103
|
+
return (
|
|
104
|
+
<div>
|
|
105
|
+
<input />
|
|
106
|
+
<p>My dog's name is {pet.name}!</p>
|
|
107
|
+
</div>
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
If you change the key `pet` to `dog`:
|
|
113
|
+
|
|
114
|
+
```diff
|
|
115
|
+
export default function Component() {
|
|
116
|
+
- const { pet } = useMyCustomHook();
|
|
117
|
+
+ const { dog } = useMyCustomHook();
|
|
118
|
+
return (
|
|
119
|
+
<div>
|
|
120
|
+
<input />
|
|
121
|
+
- <p>My dog's name is {pet.name}!</p>
|
|
122
|
+
+ <p>My dog's name is {dog.name}!</p>
|
|
123
|
+
</div>
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
then React Fast Refresh will not be able to preserve state `<input />` ❌.
|
|
129
|
+
|
|
130
|
+
### Component Keys
|
|
131
|
+
|
|
132
|
+
In some cases, React cannot distinguish between existing components being changed and new components being added. [React needs `key`s][react-keys] to disambiguate these cases and track changes when sibling elements are modified.
|
|
133
|
+
|
|
134
|
+
[virtual-dom]: https://reactjs.org/docs/faq-internals.html#what-is-the-virtual-dom
|
|
135
|
+
[react-refresh]: https://github.com/facebook/react/tree/main/packages/react-refresh
|
|
136
|
+
[react-keys]: https://react.dev/learn/rendering-lists#why-does-react-need-keys
|
|
137
|
+
[route-module]: ../start/framework/route-module
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Hydration
|
|
3
|
+
hidden: true
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
There are a few nuances worth noting around the behavior of `HydrateFallback`:
|
|
7
|
+
|
|
8
|
+
- It is only relevant on initial document request and hydration, and will not be rendered on any subsequent client-side navigations
|
|
9
|
+
- It is only relevant when you are also setting [`clientLoader.hydrate=true`][hydrate-true] on a given route
|
|
10
|
+
- It is also relevant if you do have a `clientLoader` without a server `loader`, as this implies `clientLoader.hydrate=true` since there is otherwise no loader data at all to return from `useLoaderData`
|
|
11
|
+
- Even if you do not specify a `HydrateFallback` in this case, React Router will not render your route component and will bubble up to any ancestor `HydrateFallback` component
|
|
12
|
+
- This is to ensure that `useLoaderData` remains "happy-path"
|
|
13
|
+
- Without a server `loader`, `useLoaderData` would return `undefined` in any rendered route components
|
|
14
|
+
- You cannot render an `<Outlet/>` in a `HydrateFallback` because children routes can't be guaranteed to operate correctly since their ancestor loader data may not yet be available if they are running `clientLoader` functions on hydration (i.e., use cases such as `useRouteLoaderData()` or `useMatches()`)
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Index Query Param
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
# Index Query Param
|
|
6
|
+
|
|
7
|
+
[MODES: framework, data]
|
|
8
|
+
|
|
9
|
+
## Overview
|
|
10
|
+
|
|
11
|
+
You may find a wild `?index` appearing in the URL of your app when submitting forms.
|
|
12
|
+
|
|
13
|
+
Because of nested routes, multiple routes in your route hierarchy can match the URL. Unlike navigations where all matching route [`loader`][loader]s are called to build up the UI, when a [`form`][form_element] is submitted, _only one action is called_.
|
|
14
|
+
|
|
15
|
+
Because index routes share the same URL as their parent, the `?index` param lets you disambiguate between the two.
|
|
16
|
+
|
|
17
|
+
## Understanding Index Routes
|
|
18
|
+
|
|
19
|
+
For example, consider the following route structure:
|
|
20
|
+
|
|
21
|
+
```ts filename=app/routes.ts
|
|
22
|
+
import {
|
|
23
|
+
type RouteConfig,
|
|
24
|
+
route,
|
|
25
|
+
index,
|
|
26
|
+
} from "@react-router/dev/routes";
|
|
27
|
+
|
|
28
|
+
export default [
|
|
29
|
+
route("projects", "./pages/projects.tsx", [
|
|
30
|
+
index("./pages/projects/index.tsx"),
|
|
31
|
+
route(":id", "./pages/projects/project.tsx"),
|
|
32
|
+
]),
|
|
33
|
+
] satisfies RouteConfig;
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
This creates two routes that match `/projects`:
|
|
37
|
+
|
|
38
|
+
- The parent route (`./pages/projects.tsx`)
|
|
39
|
+
- The index route (`./pages/projects/index.tsx`)
|
|
40
|
+
|
|
41
|
+
## Form Submission Targeting
|
|
42
|
+
|
|
43
|
+
For example, consider the following forms:
|
|
44
|
+
|
|
45
|
+
```tsx
|
|
46
|
+
<Form method="post" action="/projects" />
|
|
47
|
+
<Form method="post" action="/projects?index" />
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
The `?index` param will submit to the index route; the action without the index param will submit to the parent route.
|
|
51
|
+
|
|
52
|
+
When a [`<Form>`][form_component] is rendered in an index route without an [`action`][action], the `?index` param will automatically be appended so that the form posts to the index route. The following form, when submitted, will post to `/projects?index` because it is rendered in the context of the `projects` index route:
|
|
53
|
+
|
|
54
|
+
```tsx filename=app/pages/projects/index.tsx
|
|
55
|
+
function ProjectsIndex() {
|
|
56
|
+
return <Form method="post" />;
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
If you moved the code to the project layout (`./pages/projects.tsx` in this example), it would instead post to `/projects`.
|
|
61
|
+
|
|
62
|
+
This applies to `<Form>` and all of its cousins:
|
|
63
|
+
|
|
64
|
+
```tsx
|
|
65
|
+
function Component() {
|
|
66
|
+
const submit = useSubmit();
|
|
67
|
+
submit({}, { action: "/projects" });
|
|
68
|
+
submit({}, { action: "/projects?index" });
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
```tsx
|
|
73
|
+
function Component() {
|
|
74
|
+
const fetcher = useFetcher();
|
|
75
|
+
fetcher.submit({}, { action: "/projects" });
|
|
76
|
+
fetcher.submit({}, { action: "/projects?index" });
|
|
77
|
+
<fetcher.Form action="/projects" />;
|
|
78
|
+
<fetcher.Form action="/projects?index" />;
|
|
79
|
+
<fetcher.Form />; // defaults to the route in context
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
[loader]: ../api/data-routers/loader
|
|
84
|
+
[form_element]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form
|
|
85
|
+
[form_component]: ../api/components/Form
|
|
86
|
+
[action]: ../api/data-routers/action
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Lazy Route Discovery
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
# Lazy Route Discovery
|
|
6
|
+
|
|
7
|
+
[MODES: framework]
|
|
8
|
+
|
|
9
|
+
<br/>
|
|
10
|
+
<br/>
|
|
11
|
+
|
|
12
|
+
Lazy Route Discovery is a performance optimization that loads route information progressively as users navigate through your application, rather than loading the complete route manifest upfront.
|
|
13
|
+
|
|
14
|
+
With Lazy Route Discovery enabled (the default), React Router sends only the routes needed for the initial server-side render in the manifest. As users navigate to new parts of your application, additional route information is fetched dynamically and added to the client-side manifest.
|
|
15
|
+
|
|
16
|
+
The route manifest contains metadata about your routes (JavaScript/CSS imports, whether routes have `loaders`/`actions`, etc.) but not the actual route module implementations. This allows React Router to understand your application's structure without downloading unnecessary route information.
|
|
17
|
+
|
|
18
|
+
## Route Discovery Process
|
|
19
|
+
|
|
20
|
+
When a user navigates to a new route that isn't in the current manifest:
|
|
21
|
+
|
|
22
|
+
1. **Route Discovery Request** - React Router makes a request to the internal `/__manifest` endpoint
|
|
23
|
+
2. **Manifest Patch** - The server responds with the required route information
|
|
24
|
+
3. **Route Loading** - React Router loads the necessary route modules and data
|
|
25
|
+
4. **Navigation** - The user navigates to the new route
|
|
26
|
+
|
|
27
|
+
## Eager Discovery Optimization
|
|
28
|
+
|
|
29
|
+
To prevent navigation waterfalls, React Router implements eager route discovery. All [`<Link>`](../api/components/Link) and [`<NavLink>`](../api/components/NavLink) components rendered on the current page are automatically discovered via a batched request to the server.
|
|
30
|
+
|
|
31
|
+
This discovery request typically completes before users click any links, making subsequent navigation feel synchronous even with lazy route discovery enabled.
|
|
32
|
+
|
|
33
|
+
```tsx
|
|
34
|
+
// Links are automatically discovered by default
|
|
35
|
+
<Link to="/dashboard">Dashboard</Link>
|
|
36
|
+
|
|
37
|
+
// Opt out of discovery for specific links
|
|
38
|
+
<Link to="/admin" discover="none">Admin</Link>
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Performance Benefits
|
|
42
|
+
|
|
43
|
+
Lazy Route Discovery provides several performance improvements:
|
|
44
|
+
|
|
45
|
+
- **Faster Initial Load** - Smaller initial bundle size by excluding unused route metadata
|
|
46
|
+
- **Reduced Memory Usage** - Route information is loaded only when needed
|
|
47
|
+
- **Scalability** - Applications with hundreds of routes see more significant benefits
|
|
48
|
+
|
|
49
|
+
## Configuration
|
|
50
|
+
|
|
51
|
+
You can configure route discovery behavior in your `react-router.config.ts`:
|
|
52
|
+
|
|
53
|
+
```tsx filename=react-router.config.ts
|
|
54
|
+
export default {
|
|
55
|
+
// Default: lazy discovery with /__manifest endpoint
|
|
56
|
+
routeDiscovery: {
|
|
57
|
+
mode: "lazy",
|
|
58
|
+
manifestPath: "/__manifest",
|
|
59
|
+
},
|
|
60
|
+
|
|
61
|
+
// Custom manifest path (useful for multiple apps on same domain)
|
|
62
|
+
routeDiscovery: {
|
|
63
|
+
mode: "lazy",
|
|
64
|
+
manifestPath: "/my-app-manifest",
|
|
65
|
+
},
|
|
66
|
+
|
|
67
|
+
// Disable lazy discovery (include all routes initially)
|
|
68
|
+
routeDiscovery: { mode: "initial" },
|
|
69
|
+
} satisfies Config;
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Deployment Considerations
|
|
73
|
+
|
|
74
|
+
When using lazy route discovery, ensure your deployment setup handles manifest requests properly:
|
|
75
|
+
|
|
76
|
+
- **Route Handling** - Ensure `/__manifest` requests reach your React Router handler
|
|
77
|
+
- **CDN Caching** - If using CDN/edge caching, include `version` and `paths` query parameters in your cache key for the manifest endpoint
|
|
78
|
+
- **Multiple Applications** - Use a custom `manifestPath` if running multiple React Router applications on the same domain
|