Skip to content

Commit 4806039

Browse files
committed
Update docs for useLinkBuilder
1 parent 83525fc commit 4806039

File tree

3 files changed

+54
-15
lines changed

3 files changed

+54
-15
lines changed

versioned_docs/version-7.x/upgrading-from-6.x.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,18 +156,20 @@ or
156156

157157
With this change, you'd now have full type-safety when using the `Link` component given that you have [configured the global type](typescript.md#specifying-default-types-for-usenavigation-link-ref-etc).
158158
159-
### The `useLinkTo` and `useLinkBuilder` hooks are merged into `useLinkTools`
159+
### The `useLinkBuilder` hooks now returns an object instead of a function
160160
161-
Previously, the `useLinkTo` and `useLinkBuilder` hooks could be used to build a path for a screen or action for a path respectively. This was primarily useful for building custom navigators. Now, both of these hooks are merged into a single `useLinkTools` hook:
161+
Previously, the `useLinkBuilder` hooks returned a function to build a `href` for a screen - which is primarily useful for building custom navigators. Now, it returns an object with `buildHref` and `buildAction` methods:
162162
163163
```js
164-
const { buildHref, buildAction } = useLinkTools();
164+
const { buildHref, buildAction } = useLinkBuilder();
165165
166166
const href = buildHref('Details', { foo: 42 }); // '/details?foo=42'
167167
const action = buildAction('/details?foo=42'); // { type: 'NAVIGATE', payload: { name: 'Details', params: { foo: 42 } } }
168168
```
169169
170-
Note that this hook is intended to be used by custom navigators and not by end users. For end users, the `Link` component and `useLinkProps` are the recommended way to navigate.
170+
The `buildHref` method acts the same as the previously returned function. The new `buildAction` method can be used to build a navigation action from a `href` string.
171+
172+
Note that this hook is intended to be primarily used by custom navigators and not by end users. For end users, the `Link` component and `useLinkProps` are the recommended way to navigate.
171173
172174
### The flipper devtools plugin is now removed
173175

versioned_docs/version-7.x/use-link-builder.md

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,61 @@ title: useLinkBuilder
44
sidebar_label: useLinkBuilder
55
---
66

7-
The `useLinkBuilder` hook lets us build a path to use for links for a screen in the current navigator's state. It returns a function that takes `name` and `params` for the screen to focus and returns path based on the [`linking` options](navigation-container.md#linking).
7+
The `useLinkBuilder` hook returns helpers to build `href` or action based on the linking options. It returns an object with the following properties:
8+
9+
- [`buildHref`](#buildhref)
10+
- [`buildAction`](#buildaction)
11+
12+
## `buildHref`
13+
14+
The `buildHref` method lets us build a path to use for links for a screen in the current navigator's state. It returns a function that takes `name` and `params` for the screen to focus and returns path based on the [`linking` options](navigation-container.md#linking).
815

916
```js
10-
import { Link, CommonActions, useLinkBuilder } from '@react-navigation/native';
17+
import { useLinkBuilder } from '@react-navigation/native';
18+
import { PlatformPressable } from '@react-navigation/elements';
1119

1220
// ...
1321

14-
function DrawerContent({ state, descriptors }) {
15-
const buildLink = useLinkBuilder();
22+
function DrawerContent({ state, descriptors, navigation }) {
23+
const { buildHref } = useLinkBuilder();
1624

1725
return state.routes((route) => (
18-
<Link
19-
to={buildLink(route.name, route.params)}
20-
action={CommonActions.navigate(route.name)}
26+
<PlatformPressable
27+
href={buildHref(route.name, route.params)}
28+
onPress={() => navigation.navigate(route.name, route.params)}
2129
>
2230
{descriptors[route.key].options.title}
23-
</Link>
31+
</PlatformPressable>
2432
));
2533
}
2634
```
2735

28-
This hook is intended to be used in navigators to show links to various pages in it, such as drawer and tab navigators. If you're building a custom navigator, custom drawer content, custom tab bar etc. then you might want to use this hook.
36+
This hook is intended to be used in navigators to show links to various pages in the navigator, such as drawer and tab navigators. If you're building a custom navigator, custom drawer content, custom tab bar etc. then you might want to use this hook.
2937

3038
There are couple of important things to note:
3139

3240
- The destination screen must be present in the current navigator. It cannot be in a parent navigator or a navigator nested in a child.
33-
- It's intended to be only used in custom navigators to keep them reusable in multiple apps. For your regular app code, use paths directly instead of building paths for screens, or use [`Link`](link.md) and [`useLinkProps`](use-link-props.md) which transparently handle paths.
41+
- It's intended to be only used in custom navigators to keep them reusable in multiple apps. For your regular app code, use screen names directly instead of building paths for screens.
42+
43+
## `buildAction`
44+
45+
The `buildAction` method lets us parse a `href` string into an action object that can be used with [`navigation.dispatch`](navigation-prop.md#dispatch) to navigate to the relevant screen.
46+
47+
```js
48+
import { Link, CommonActions, useLinkBuilder } from '@react-navigation/native';
49+
import { Button } from '@react-navigation/elements';
50+
51+
// ...
52+
53+
function MyComponent() {
54+
const { buildAction } = useLinkBuilder();
55+
56+
return (
57+
<Button onPress={() => navigation.dispatch(buildAction('/users/jane'))}>
58+
Go to Jane's profile
59+
</Button>
60+
);
61+
}
62+
```
63+
64+
The [`useLinkTo`](use-link-to.md) hook is a convenient wrapper around this hook to navigate to a screen using a `href` string.

versioned_docs/version-7.x/use-link-to.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ function Home() {
2222
}
2323
```
2424
25-
This is a low-level hook used to build more complex behavior on top. We recommended to use the [`useLinkProps` hook](use-link-props.md) to build your custom link components instead of using this hook directly. It will ensure that your component is properly accessible on the web.
25+
This is a low-level hook used to build more complex behavior on top. We recommended using the [`useLinkProps` hook](use-link-props.md) to build your custom link components instead of using this hook directly. It will ensure that your component is properly accessible on the web.
26+
27+
:::warning
28+
29+
Navigating via `href` strings is not type-safe. If you want to navigate to a screen with type-safety, it's recommended to use screen names directly.
30+
31+
:::
2632

2733
## Using with class component
2834

0 commit comments

Comments
 (0)