Skip to content

Commit

Permalink
docs: Link to property
Browse files Browse the repository at this point in the history
  • Loading branch information
schiller-manuel committed Sep 12, 2024
1 parent d328172 commit 56ce177
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/framework/react/guide/navigation.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ It's also common to want to update a single search param without supplying any o
```tsx
const link = (
<Link
to="."
search={(prev) => ({
...prev,
page: prev.page + 1,
Expand Down
41 changes: 40 additions & 1 deletion docs/framework/react/guide/search-params.md
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,10 @@ Now that you've learned how to read your route's search params, you'll be happy

### `<Link search />`

The best way to update search params is to use the `search` prop on the `<Link />` component. Remember, if a `to` prop is omitted, will update the search for the current page. Here's an example:
The best way to update search params is to use the `search` prop on the `<Link />` component.

If the search for the current page shall be updated and the `from` prop is specified, the `to` prop can be omitted.
Here's an example:

```tsx
// /routes/shop.products.tsx
Expand All @@ -449,6 +452,42 @@ const ProductList = () => {
}
```

If you want to update the search params in a generic component that is rendered on multiple routes, specifiying `from` can be challenging.

In this scenario you can set `to="."` which will give you access to loosely typed search params.
Here is an example that illustrates this:

```tsx
// `page` is a search param that is defined in the __root route and hence available on all routes.
const PageSelector = () => {
return (
<div>
<Link to="." search={(prev) => ({ ...prev, page: prev.page + 1 })}>
Next Page
</Link>
</div>
)
}
```

If the generic component is only rendered in a specific subtree of the route tree, you can specify that subtree using `from`. Here you can omit `to='.'` if you want.

```tsx
// `page` is a search param that is defined in the /posts route and hence available on all of its child routes.
const PageSelector = () => {
return (
<div>
<Link
from="/posts"
to="."
search={(prev) => ({ ...prev, page: prev.page + 1 })}
>
Next Page
</Link>
</div>
)
```
### `useNavigate(), navigate({ search })`
The `navigate` function also accepts a `search` option that works the same way as the `search` prop on `<Link />`:
Expand Down
7 changes: 3 additions & 4 deletions docs/framework/react/guide/type-safety.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,11 @@ Consider the following usage of `Link`
```tsx
<Link to=".." search={{ page: 0 }} />
<Link to="." search={{ page: 0 }} />
<Link search={{ page: 0 }} />
```

**These examples are bad for TS performance**. That's because `search` resolves to a union of all `search` params for all routes and TS has to check whatever you pass to the `search` prop against this potentially big union. As your application grows, this check time will increase linearly to number of routes and search params. We have done our best to optimize for this case (TypeScript will typically do this work once and cache it) but the initial check against this large union is expensive. This also applies to `params` and other API's such as `useSearch`, `useParams`, `useNavigate` etc
**These examples are bad for TS performance**. That's because `search` resolves to a union of all `search` params for all routes and TS has to check whatever you pass to the `search` prop against this potentially big union. As your application grows, this check time will increase linearly to number of routes and search params. We have done our best to optimize for this case (TypeScript will typically do this work once and cache it) but the initial check against this large union is expensive. This also applies to `params` and other API's such as `useSearch`, `useParams`, `useNavigate` etc.

Instead you should try to narrow to relevant routes with `from` or `to`
Instead you should try to narrow to relevant routes with `from` or `to`.

```tsx
<Link from={Route.fullPath} to=".." search={{page: 0}} />
Expand All @@ -180,7 +179,7 @@ const from: '/posts/$postId/deep' | '/posts/' = '/posts/'
<Link from={from} to='..' />
```

You can also pass branches to `from` to only resolve `search` or `params` to be from any descendants of that branch
You can also pass branches to `from` to only resolve `search` or `params` to be from any descendants of that branch:

```tsx
const from = '/posts'
Expand Down

0 comments on commit 56ce177

Please sign in to comment.