diff --git a/docs/components/link.md b/docs/components/link.md index 3fccea2ee3..86b2f95c8e 100644 --- a/docs/components/link.md +++ b/docs/components/link.md @@ -67,9 +67,28 @@ A relative `` value (that does not begin with `/`) resolves relative to ## `relative` -By default, links are relative to the route hierarchy (`relative="route"`), so `..` will go up one `Route` level from the current contextual route. Occasionally, you may find that you have matching URL patterns that do not make sense to be nested, and you'd prefer to use relative _path_ routing from the current contextual route path. You can opt into this behavior with `relative="path"`: +By default, links are relative to the route hierarchy (`relative="route"`), so `..` will go up one `Route` level from the current contextual route. However, `index` routes (or any other routes that don't add to the path) are skipped. -```jsx +```tsx + + } /> + + } /> + } /> + } /> + +; + +function FeatureA() { + return Back; +} +``` + +Let's assume the current path is `/main/module`. When you click the "Back" link, it navigates one level up in the route hierarchy, skipping routes such as `index` that don't add to the path. As a result, the path becomes `/main`. Now, let's assume the current path is `/main/module/feature-a`. When you click the "Back" link, the path becomes `/main/module`. + +Occasionally, you may find that you have matching URL patterns that do not make sense to be nested, and you'd prefer to use relative _path_ routing from the current contextual route path. You can opt into this behavior with `relative="path"`: + +```tsx // Contact and EditContact do not share additional UI layout }> } /> @@ -91,11 +110,11 @@ function EditContact() { } ``` -Please note that `relative: "path"` only impacts the resolution of a relative path. It does not change the "starting" location for that relative path resolution. This resolution is always relative to the current location in the Route hierarchy (i.e., the route `Link` is rendered in). +Please note that `relative="path"` only impacts the resolution of a relative path. It does not change the "starting" location for that relative path resolution. This resolution is always relative to the current location in the Route hierarchy (i.e., the route `Link` is rendered in). If you wish to use path-relative routing against the current URL instead of the route hierarchy, you can do that with the current [`location`][use-location] and the `URL` constructor (note the trailing slash behavior): -```js +```ts // Assume the current URL is https://remix.run/docs/en/main/start/quickstart let location = useLocation();