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,281 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Routing
|
|
3
|
+
order: 2
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Routing
|
|
7
|
+
|
|
8
|
+
[MODES: data]
|
|
9
|
+
|
|
10
|
+
## Configuring Routes
|
|
11
|
+
|
|
12
|
+
Routes are configured as the first argument to `createBrowserRouter`. At a minimum, you need a path and component:
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
import { createBrowserRouter } from "react-router";
|
|
16
|
+
|
|
17
|
+
function Root() {
|
|
18
|
+
return <h1>Hello world</h1>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const router = createBrowserRouter([
|
|
22
|
+
{ path: "/", Component: Root },
|
|
23
|
+
]);
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Here is a larger sample route config:
|
|
27
|
+
|
|
28
|
+
```ts filename=app/routes.ts
|
|
29
|
+
createBrowserRouter([
|
|
30
|
+
{
|
|
31
|
+
path: "/",
|
|
32
|
+
Component: Root,
|
|
33
|
+
children: [
|
|
34
|
+
{ index: true, Component: Home },
|
|
35
|
+
{ path: "about", Component: About },
|
|
36
|
+
{
|
|
37
|
+
path: "auth",
|
|
38
|
+
Component: AuthLayout,
|
|
39
|
+
children: [
|
|
40
|
+
{ path: "login", Component: Login },
|
|
41
|
+
{ path: "register", Component: Register },
|
|
42
|
+
],
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
path: "concerts",
|
|
46
|
+
children: [
|
|
47
|
+
{ index: true, Component: ConcertsHome },
|
|
48
|
+
{ path: ":city", Component: ConcertsCity },
|
|
49
|
+
{ path: "trending", Component: ConcertsTrending },
|
|
50
|
+
],
|
|
51
|
+
},
|
|
52
|
+
],
|
|
53
|
+
},
|
|
54
|
+
]);
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Route Objects
|
|
58
|
+
|
|
59
|
+
Route objects define the behavior of a route beyond just the path and component, like data loading and actions. We'll go into more detail in the [Route Object guide](./route-object), but here's a quick example of a loader.
|
|
60
|
+
|
|
61
|
+
```tsx filename=app/team.tsx
|
|
62
|
+
import {
|
|
63
|
+
createBrowserRouter,
|
|
64
|
+
useLoaderData,
|
|
65
|
+
} from "react-router";
|
|
66
|
+
|
|
67
|
+
createBrowserRouter([
|
|
68
|
+
{
|
|
69
|
+
path: "/teams/:teamId",
|
|
70
|
+
loader: async ({ params }) => {
|
|
71
|
+
let team = await fetchTeam(params.teamId);
|
|
72
|
+
return { name: team.name };
|
|
73
|
+
},
|
|
74
|
+
Component: Team,
|
|
75
|
+
},
|
|
76
|
+
]);
|
|
77
|
+
|
|
78
|
+
function Team() {
|
|
79
|
+
let data = useLoaderData();
|
|
80
|
+
return <h1>{data.name}</h1>;
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Nested Routes
|
|
85
|
+
|
|
86
|
+
Routes can be nested inside parent routes through `children`.
|
|
87
|
+
|
|
88
|
+
```ts filename=app/routes.ts
|
|
89
|
+
createBrowserRouter([
|
|
90
|
+
{
|
|
91
|
+
path: "/dashboard",
|
|
92
|
+
Component: Dashboard,
|
|
93
|
+
children: [
|
|
94
|
+
{ index: true, Component: Home },
|
|
95
|
+
{ path: "settings", Component: Settings },
|
|
96
|
+
],
|
|
97
|
+
},
|
|
98
|
+
]);
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
The path of the parent is automatically included in the child, so this config creates both `"/dashboard"` and `"/dashboard/settings"` URLs.
|
|
102
|
+
|
|
103
|
+
Child routes are rendered through the `<Outlet/>` in the parent route.
|
|
104
|
+
|
|
105
|
+
```tsx filename=app/dashboard.tsx
|
|
106
|
+
import { Outlet } from "react-router";
|
|
107
|
+
|
|
108
|
+
export default function Dashboard() {
|
|
109
|
+
return (
|
|
110
|
+
<div>
|
|
111
|
+
<h1>Dashboard</h1>
|
|
112
|
+
{/* will either be <Home> or <Settings> */}
|
|
113
|
+
<Outlet />
|
|
114
|
+
</div>
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## Layout Routes
|
|
120
|
+
|
|
121
|
+
Omitting the `path` in a route creates new [Nested Routes](#nested-routes) for its children without adding any segments to the URL.
|
|
122
|
+
|
|
123
|
+
```tsx lines=[3,16]
|
|
124
|
+
createBrowserRouter([
|
|
125
|
+
{
|
|
126
|
+
// no path on this parent route, just the component
|
|
127
|
+
Component: MarketingLayout,
|
|
128
|
+
children: [
|
|
129
|
+
{ index: true, Component: Home },
|
|
130
|
+
{ path: "contact", Component: Contact },
|
|
131
|
+
],
|
|
132
|
+
},
|
|
133
|
+
|
|
134
|
+
{
|
|
135
|
+
path: "projects",
|
|
136
|
+
children: [
|
|
137
|
+
{ index: true, Component: ProjectsHome },
|
|
138
|
+
{
|
|
139
|
+
// again, no path, just a component for the layout
|
|
140
|
+
Component: ProjectLayout,
|
|
141
|
+
children: [
|
|
142
|
+
{ path: ":pid", Component: Project },
|
|
143
|
+
{ path: ":pid/edit", Component: EditProject },
|
|
144
|
+
],
|
|
145
|
+
},
|
|
146
|
+
],
|
|
147
|
+
},
|
|
148
|
+
]);
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
Note that:
|
|
152
|
+
|
|
153
|
+
- `Home` and `Contact` will be rendered into the `MarketingLayout` outlet
|
|
154
|
+
- `Project` and `EditProject` will be rendered into the `ProjectLayout` outlet while `ProjectsHome` will not.
|
|
155
|
+
|
|
156
|
+
## Index Routes
|
|
157
|
+
|
|
158
|
+
Index routes are defined by setting `index: true` on a route object without a path.
|
|
159
|
+
|
|
160
|
+
```ts
|
|
161
|
+
{ index: true, Component: Home }
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
Index routes render into their parent's [Outlet][outlet] at their parent's URL (like a default child route).
|
|
165
|
+
|
|
166
|
+
```ts lines=[4,5,10,11]
|
|
167
|
+
import { createBrowserRouter } from "react-router";
|
|
168
|
+
|
|
169
|
+
createBrowserRouter([
|
|
170
|
+
// renders at "/"
|
|
171
|
+
{ index: true, Component: Home },
|
|
172
|
+
{
|
|
173
|
+
Component: Dashboard,
|
|
174
|
+
path: "/dashboard",
|
|
175
|
+
children: [
|
|
176
|
+
// renders at "/dashboard"
|
|
177
|
+
{ index: true, Component: DashboardHome },
|
|
178
|
+
{ path: "settings", Component: DashboardSettings },
|
|
179
|
+
],
|
|
180
|
+
},
|
|
181
|
+
]);
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
Note that index routes can't have children.
|
|
185
|
+
|
|
186
|
+
## Prefix Route
|
|
187
|
+
|
|
188
|
+
A route with just a path and no component creates a group of routes with a path prefix.
|
|
189
|
+
|
|
190
|
+
```tsx lines=[3]
|
|
191
|
+
createBrowserRouter([
|
|
192
|
+
{
|
|
193
|
+
// no component, just a path
|
|
194
|
+
path: "/projects",
|
|
195
|
+
children: [
|
|
196
|
+
{ index: true, Component: ProjectsHome },
|
|
197
|
+
{ path: ":pid", Component: Project },
|
|
198
|
+
{ path: ":pid/edit", Component: EditProject },
|
|
199
|
+
],
|
|
200
|
+
},
|
|
201
|
+
]);
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
This creates the routes `/projects`, `/projects/:pid`, and `/projects/:pid/edit` without introducing a layout component.
|
|
205
|
+
|
|
206
|
+
## Dynamic Segments
|
|
207
|
+
|
|
208
|
+
If a path segment starts with `:` then it becomes a "dynamic segment". When the route matches the URL, the dynamic segment will be parsed from the URL and provided as `params` to other router APIs.
|
|
209
|
+
|
|
210
|
+
```ts lines=[2]
|
|
211
|
+
{
|
|
212
|
+
path: "teams/:teamId",
|
|
213
|
+
loader: async ({ params }) => {
|
|
214
|
+
// params are available in loaders/actions
|
|
215
|
+
let team = await fetchTeam(params.teamId);
|
|
216
|
+
return { name: team.name };
|
|
217
|
+
},
|
|
218
|
+
Component: Team,
|
|
219
|
+
}
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
```tsx
|
|
223
|
+
import { useParams } from "react-router";
|
|
224
|
+
|
|
225
|
+
function Team() {
|
|
226
|
+
// params are available in components through useParams
|
|
227
|
+
let params = useParams();
|
|
228
|
+
// ...
|
|
229
|
+
}
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
You can have multiple dynamic segments in one route path:
|
|
233
|
+
|
|
234
|
+
```ts
|
|
235
|
+
{
|
|
236
|
+
path: "c/:categoryId/p/:productId";
|
|
237
|
+
}
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
## Optional Segments
|
|
241
|
+
|
|
242
|
+
You can make a route segment optional by adding a `?` to the end of the segment.
|
|
243
|
+
|
|
244
|
+
```ts
|
|
245
|
+
{
|
|
246
|
+
path: ":lang?/categories";
|
|
247
|
+
}
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
You can have optional static segments, too:
|
|
251
|
+
|
|
252
|
+
```ts
|
|
253
|
+
{
|
|
254
|
+
path: "users/:userId/edit?";
|
|
255
|
+
}
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
## Splats
|
|
259
|
+
|
|
260
|
+
Also known as "catchall" and "star" segments. If a route path pattern ends with `/*` then it will match any characters following the `/`, including other `/` characters.
|
|
261
|
+
|
|
262
|
+
```ts
|
|
263
|
+
{
|
|
264
|
+
path: "files/*";
|
|
265
|
+
loader: async ({ params }) => {
|
|
266
|
+
params["*"]; // will contain the remaining URL after files/
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
You can destructure the `*`, you just have to assign it a new name. A common name is `splat`:
|
|
272
|
+
|
|
273
|
+
```tsx
|
|
274
|
+
const { "*": splat } = params;
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
---
|
|
278
|
+
|
|
279
|
+
Next: [Route Object](./route-object)
|
|
280
|
+
|
|
281
|
+
[outlet]: https://api.reactrouter.com/v7/functions/react-router.Outlet.html
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Installation
|
|
3
|
+
order: 1
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Installation
|
|
7
|
+
|
|
8
|
+
[MODES: declarative]
|
|
9
|
+
|
|
10
|
+
## Introduction
|
|
11
|
+
|
|
12
|
+
You can start with a React template from Vite and choose "React", otherwise bootstrap your application however you prefer.
|
|
13
|
+
|
|
14
|
+
```shellscript nonumber
|
|
15
|
+
npx create-vite@latest
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Next install React Router from npm:
|
|
19
|
+
|
|
20
|
+
```shellscript nonumber
|
|
21
|
+
npm i react-router
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Finally, render a `<BrowserRouter>` around your application:
|
|
25
|
+
|
|
26
|
+
```tsx lines=[3,9-11]
|
|
27
|
+
import React from "react";
|
|
28
|
+
import ReactDOM from "react-dom/client";
|
|
29
|
+
import { BrowserRouter } from "react-router";
|
|
30
|
+
import App from "./app";
|
|
31
|
+
|
|
32
|
+
const root = document.getElementById("root");
|
|
33
|
+
|
|
34
|
+
ReactDOM.createRoot(root).render(
|
|
35
|
+
<BrowserRouter>
|
|
36
|
+
<App />
|
|
37
|
+
</BrowserRouter>,
|
|
38
|
+
);
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
Next: [Routing](./routing)
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Navigating
|
|
3
|
+
order: 3
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Navigating
|
|
7
|
+
|
|
8
|
+
[MODES: declarative]
|
|
9
|
+
|
|
10
|
+
## Introduction
|
|
11
|
+
|
|
12
|
+
Users navigate your application with `<Link>`, `<NavLink>`, and `useNavigate`.
|
|
13
|
+
|
|
14
|
+
## NavLink
|
|
15
|
+
|
|
16
|
+
This component is for navigation links that need to render an active state.
|
|
17
|
+
|
|
18
|
+
```tsx
|
|
19
|
+
import { NavLink } from "react-router";
|
|
20
|
+
|
|
21
|
+
export function MyAppNav() {
|
|
22
|
+
return (
|
|
23
|
+
<nav>
|
|
24
|
+
<NavLink to="/" end>
|
|
25
|
+
Home
|
|
26
|
+
</NavLink>
|
|
27
|
+
<NavLink to="/trending" end>
|
|
28
|
+
Trending Concerts
|
|
29
|
+
</NavLink>
|
|
30
|
+
<NavLink to="/concerts">All Concerts</NavLink>
|
|
31
|
+
<NavLink to="/account">Account</NavLink>
|
|
32
|
+
</nav>
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Whenever a `NavLink` is active, it will automatically have an `.active` class name for easy styling with CSS:
|
|
38
|
+
|
|
39
|
+
```css
|
|
40
|
+
a.active {
|
|
41
|
+
color: red;
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
It also has callback props on `className`, `style`, and `children` with the active state for inline styling or conditional rendering:
|
|
46
|
+
|
|
47
|
+
```tsx
|
|
48
|
+
// className
|
|
49
|
+
<NavLink
|
|
50
|
+
to="/messages"
|
|
51
|
+
className={({ isActive }) =>
|
|
52
|
+
isActive ? "text-red-500" : "text-black"
|
|
53
|
+
}
|
|
54
|
+
>
|
|
55
|
+
Messages
|
|
56
|
+
</NavLink>
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
```tsx
|
|
60
|
+
// style
|
|
61
|
+
<NavLink
|
|
62
|
+
to="/messages"
|
|
63
|
+
style={({ isActive }) => ({
|
|
64
|
+
color: isActive ? "red" : "black",
|
|
65
|
+
})}
|
|
66
|
+
>
|
|
67
|
+
Messages
|
|
68
|
+
</NavLink>
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
```tsx
|
|
72
|
+
// children
|
|
73
|
+
<NavLink to="/message">
|
|
74
|
+
{({ isActive }) => (
|
|
75
|
+
<span className={isActive ? "active" : ""}>
|
|
76
|
+
{isActive ? "👉" : ""} Tasks
|
|
77
|
+
</span>
|
|
78
|
+
)}
|
|
79
|
+
</NavLink>
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Link
|
|
83
|
+
|
|
84
|
+
Use `<Link>` when the link doesn't need active styling:
|
|
85
|
+
|
|
86
|
+
```tsx
|
|
87
|
+
import { Link } from "react-router";
|
|
88
|
+
|
|
89
|
+
export function LoggedOutMessage() {
|
|
90
|
+
return (
|
|
91
|
+
<p>
|
|
92
|
+
You've been logged out.{" "}
|
|
93
|
+
<Link to="/login">Login again</Link>
|
|
94
|
+
</p>
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## useNavigate
|
|
100
|
+
|
|
101
|
+
This hook allows the programmer to navigate the user to a new page without the user interacting.
|
|
102
|
+
|
|
103
|
+
For normal navigation, it's best to use `Link` or `NavLink`. They provide a better default user experience like keyboard events, accessibility labeling, "open in new window", right click context menus, etc.
|
|
104
|
+
|
|
105
|
+
Reserve usage of `useNavigate` to situations where the user is _not_ interacting but you need to navigate, for example:
|
|
106
|
+
|
|
107
|
+
- After a form submission completes
|
|
108
|
+
- Logging them out after inactivity
|
|
109
|
+
- Timed UIs like quizzes, etc.
|
|
110
|
+
|
|
111
|
+
```tsx
|
|
112
|
+
import { useNavigate } from "react-router";
|
|
113
|
+
|
|
114
|
+
export function LoginPage() {
|
|
115
|
+
let navigate = useNavigate();
|
|
116
|
+
|
|
117
|
+
return (
|
|
118
|
+
<>
|
|
119
|
+
<MyHeader />
|
|
120
|
+
<MyLoginForm
|
|
121
|
+
onSuccess={() => {
|
|
122
|
+
navigate("/dashboard");
|
|
123
|
+
}}
|
|
124
|
+
/>
|
|
125
|
+
<MyFooter />
|
|
126
|
+
</>
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
---
|
|
132
|
+
|
|
133
|
+
Next: [Url values](./url-values)
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Routing
|
|
3
|
+
order: 2
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Routing
|
|
7
|
+
|
|
8
|
+
[MODES: declarative]
|
|
9
|
+
|
|
10
|
+
## Configuring Routes
|
|
11
|
+
|
|
12
|
+
Routes are configured by rendering `<Routes>` and `<Route>` that couple URL segments to UI elements.
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
import React from "react";
|
|
16
|
+
import ReactDOM from "react-dom/client";
|
|
17
|
+
import { BrowserRouter, Routes, Route } from "react-router";
|
|
18
|
+
import App from "./app";
|
|
19
|
+
|
|
20
|
+
const root = document.getElementById("root");
|
|
21
|
+
|
|
22
|
+
ReactDOM.createRoot(root).render(
|
|
23
|
+
<BrowserRouter>
|
|
24
|
+
<Routes>
|
|
25
|
+
<Route path="/" element={<App />} />
|
|
26
|
+
</Routes>
|
|
27
|
+
</BrowserRouter>,
|
|
28
|
+
);
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Here's a larger sample config:
|
|
32
|
+
|
|
33
|
+
```tsx
|
|
34
|
+
<Routes>
|
|
35
|
+
<Route index element={<Home />} />
|
|
36
|
+
<Route path="about" element={<About />} />
|
|
37
|
+
|
|
38
|
+
<Route element={<AuthLayout />}>
|
|
39
|
+
<Route path="login" element={<Login />} />
|
|
40
|
+
<Route path="register" element={<Register />} />
|
|
41
|
+
</Route>
|
|
42
|
+
|
|
43
|
+
<Route path="concerts">
|
|
44
|
+
<Route index element={<ConcertsHome />} />
|
|
45
|
+
<Route path=":city" element={<City />} />
|
|
46
|
+
<Route path="trending" element={<Trending />} />
|
|
47
|
+
</Route>
|
|
48
|
+
</Routes>
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Nested Routes
|
|
52
|
+
|
|
53
|
+
Routes can be nested inside parent routes.
|
|
54
|
+
|
|
55
|
+
```tsx
|
|
56
|
+
<Routes>
|
|
57
|
+
<Route path="dashboard" element={<Dashboard />}>
|
|
58
|
+
<Route index element={<Home />} />
|
|
59
|
+
<Route path="settings" element={<Settings />} />
|
|
60
|
+
</Route>
|
|
61
|
+
</Routes>
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
The path of the parent is automatically included in the child, so this config creates both `"/dashboard"` and `"/dashboard/settings"` URLs.
|
|
65
|
+
|
|
66
|
+
Child routes are rendered through the `<Outlet/>` in the parent route.
|
|
67
|
+
|
|
68
|
+
```tsx filename=app/dashboard.tsx
|
|
69
|
+
import { Outlet } from "react-router";
|
|
70
|
+
|
|
71
|
+
export default function Dashboard() {
|
|
72
|
+
return (
|
|
73
|
+
<div>
|
|
74
|
+
<h1>Dashboard</h1>
|
|
75
|
+
{/* will either be <Home/> or <Settings/> */}
|
|
76
|
+
<Outlet />
|
|
77
|
+
</div>
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Layout Routes
|
|
83
|
+
|
|
84
|
+
Routes _without_ a `path` create new nesting for their children, but they don't add any segments to the URL.
|
|
85
|
+
|
|
86
|
+
```tsx lines=[2,9]
|
|
87
|
+
<Routes>
|
|
88
|
+
<Route element={<MarketingLayout />}>
|
|
89
|
+
<Route index element={<MarketingHome />} />
|
|
90
|
+
<Route path="contact" element={<Contact />} />
|
|
91
|
+
</Route>
|
|
92
|
+
|
|
93
|
+
<Route path="projects">
|
|
94
|
+
<Route index element={<ProjectsHome />} />
|
|
95
|
+
<Route element={<ProjectsLayout />}>
|
|
96
|
+
<Route path=":pid" element={<Project />} />
|
|
97
|
+
<Route path=":pid/edit" element={<EditProject />} />
|
|
98
|
+
</Route>
|
|
99
|
+
</Route>
|
|
100
|
+
</Routes>
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Index Routes
|
|
104
|
+
|
|
105
|
+
Index routes render into their parent's `<Outlet/>` at their parent's URL (like a default child route). They are configured with the `index` prop:
|
|
106
|
+
|
|
107
|
+
```tsx lines=[4,8]
|
|
108
|
+
<Routes>
|
|
109
|
+
<Route path="/" element={<Root />}>
|
|
110
|
+
{/* renders into the outlet in <Root> at "/" */}
|
|
111
|
+
<Route index element={<Home />} />
|
|
112
|
+
|
|
113
|
+
<Route path="dashboard" element={<Dashboard />}>
|
|
114
|
+
{/* renders into the outlet in <Dashboard> at "/dashboard" */}
|
|
115
|
+
<Route index element={<DashboardHome />} />
|
|
116
|
+
<Route path="settings" element={<Settings />} />
|
|
117
|
+
</Route>
|
|
118
|
+
</Route>
|
|
119
|
+
</Routes>
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Note that index routes can't have children. If you're expecting that behavior, you probably want a [layout route](#layout-routes).
|
|
123
|
+
|
|
124
|
+
## Route Prefixes
|
|
125
|
+
|
|
126
|
+
A `<Route path>` _without_ an `element` prop adds a path prefix to its child routes, without introducing a parent layout.
|
|
127
|
+
|
|
128
|
+
```tsx filename=app/routes.ts lines=[1]
|
|
129
|
+
<Route path="projects">
|
|
130
|
+
<Route index element={<ProjectsHome />} />
|
|
131
|
+
<Route element={<ProjectsLayout />}>
|
|
132
|
+
<Route path=":pid" element={<Project />} />
|
|
133
|
+
<Route path=":pid/edit" element={<EditProject />} />
|
|
134
|
+
</Route>
|
|
135
|
+
</Route>
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
## Dynamic Segments
|
|
139
|
+
|
|
140
|
+
If a path segment starts with `:` then it becomes a "dynamic segment". When the route matches the URL, the dynamic segment will be parsed from the URL and provided as `params` to other router APIs like `useParams`.
|
|
141
|
+
|
|
142
|
+
```tsx
|
|
143
|
+
<Route path="teams/:teamId" element={<Team />} />
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
```tsx filename=app/team.tsx
|
|
147
|
+
import { useParams } from "react-router";
|
|
148
|
+
|
|
149
|
+
export default function Team() {
|
|
150
|
+
let params = useParams();
|
|
151
|
+
// params.teamId
|
|
152
|
+
}
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
You can have multiple dynamic segments in one route path:
|
|
156
|
+
|
|
157
|
+
```tsx
|
|
158
|
+
<Route
|
|
159
|
+
path="/c/:categoryId/p/:productId"
|
|
160
|
+
element={<Product />}
|
|
161
|
+
/>
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
```tsx filename=app/category-product.tsx
|
|
165
|
+
import { useParams } from "react-router";
|
|
166
|
+
|
|
167
|
+
export default function CategoryProduct() {
|
|
168
|
+
let { categoryId, productId } = useParams();
|
|
169
|
+
// ...
|
|
170
|
+
}
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
You should ensure that all dynamic segments in a given path are unique. Otherwise, as the `params` object is populated - latter dynamic segment values will override earlier values.
|
|
174
|
+
|
|
175
|
+
## Optional Segments
|
|
176
|
+
|
|
177
|
+
You can make a route segment optional by adding a `?` to the end of the segment.
|
|
178
|
+
|
|
179
|
+
```tsx
|
|
180
|
+
<Route path=":lang?/categories" element={<Categories />} />
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
You can have optional static segments, too:
|
|
184
|
+
|
|
185
|
+
```tsx
|
|
186
|
+
<Route path="users/:userId/edit?" element={<User />} />
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
## Splats
|
|
190
|
+
|
|
191
|
+
Also known as "catchall" and "star" segments. If a route path pattern ends with `/*` then it will match any characters following the `/`, including other `/` characters.
|
|
192
|
+
|
|
193
|
+
```tsx
|
|
194
|
+
<Route path="files/*" element={<File />} />
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
```tsx
|
|
198
|
+
let params = useParams();
|
|
199
|
+
// params["*"] will contain the remaining URL after files/
|
|
200
|
+
let filePath = params["*"];
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
You can destructure the `*`, you just have to assign it a new name. A common name is `splat`:
|
|
204
|
+
|
|
205
|
+
```tsx
|
|
206
|
+
let { "*": splat } = useParams();
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
## Linking
|
|
210
|
+
|
|
211
|
+
Link to routes from your UI with `Link` and `NavLink`
|
|
212
|
+
|
|
213
|
+
```tsx
|
|
214
|
+
import { NavLink, Link } from "react-router";
|
|
215
|
+
|
|
216
|
+
function Header() {
|
|
217
|
+
return (
|
|
218
|
+
<nav>
|
|
219
|
+
{/* NavLink makes it easy to show active states */}
|
|
220
|
+
<NavLink
|
|
221
|
+
to="/"
|
|
222
|
+
className={({ isActive }) =>
|
|
223
|
+
isActive ? "active" : ""
|
|
224
|
+
}
|
|
225
|
+
>
|
|
226
|
+
Home
|
|
227
|
+
</NavLink>
|
|
228
|
+
|
|
229
|
+
<Link to="/concerts/salt-lake-city">Concerts</Link>
|
|
230
|
+
</nav>
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
---
|
|
236
|
+
|
|
237
|
+
Next: [Navigating](./navigating)
|