Skip to content

Commit c7d8d86

Browse files
committed
Update docs for Link and useLinkProps
1 parent 7917e93 commit c7d8d86

File tree

2 files changed

+46
-52
lines changed

2 files changed

+46
-52
lines changed

versioned_docs/version-7.x/link.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Link
44
sidebar_label: Link
55
---
66

7-
The `Link` component renders a component that can navigate to a screen on press. This renders an `<a>` tag when using on the Web and It uses a `Text` component on other platforms. It preserves the default behavior of anchor tags in the browser such as `Right click -> Open link in new tab"`, `Ctrl+Click`/`⌘+Click` etc. to provide a native experience.
7+
The `Link` component renders a component that can navigate to a screen on press. This renders a `<a>` tag when used on the Web and uses a `Text` component on other platforms. It preserves the default behavior of anchor tags in the browser such as `Right click -> Open link in new tab"`, `Ctrl+Click`/`⌘+Click` etc. to provide a native experience.
88

99
The path in the `href` for the `<a>` tag is generated based on your [`linking` options](navigation-container.md#linking).
1010

@@ -17,13 +17,13 @@ import { Link } from '@react-navigation/native';
1717

1818
function Home() {
1919
return (
20-
<Link to={{ screen: 'Profile', params: { id: 'jane' } }}>
20+
<Link screen="Profile" params={{ id: 'jane' }}>
2121
Go to Jane's profile
2222
</Link>
2323
);
2424
}
2525
```
2626
27-
If you want to use your own custom touchable, you can use [`useLinkProps`](use-link-props.md) instead.
27+
If you want to use your own custom link component, you can use [`useLinkProps`](use-link-props.md) instead.
2828
2929
The `Link` component accepts the [same props as `useLinkProps`](use-link-props.md#options)

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

+43-49
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ title: useLinkProps
44
sidebar_label: useLinkProps
55
---
66

7-
The `useLinkProps` hook let's build our custom link components which let us navigate to a screen using a path instead of a screen name based on the [`linking` options](navigation-container.md#linking). It takes a path and returns an object with some props that you can pass to a component.
7+
The `useLinkProps` hook lets us build our custom link component. The link component can be used as a button to navigate to a screen. On the web, it will be rendered as an anchor tag (`<a>`) with the `href` attribute so that all the accessibility features of a link are preserved, e.g. - such as `Right click -> Open link in new tab"`, `Ctrl+Click`/`⌘+Click` etc.
8+
9+
It returns an object with some props that you can pass to a component.
810

911
Example:
1012

@@ -13,82 +15,54 @@ import { useLinkProps } from '@react-navigation/native';
1315

1416
// ...
1517

16-
const LinkButton = ({ to, action, children, ...rest }) => {
17-
const { onPress, ...props } = useLinkProps({ to, action });
18+
const LinkButton = ({ screen, params, action, href, children, ...rest }) => {
19+
const props = useLinkProps({ screen, params, action, href });
1820

1921
const [isHovered, setIsHovered] = React.useState(false);
2022

21-
if (Platform.OS === 'web') {
22-
// It's important to use a `View` or `Text` on web instead of `TouchableX`
23-
// Otherwise React Native for Web omits the `onClick` prop that's passed
24-
// You'll also need to pass `onPress` as `onClick` to the `View`
25-
// You can add hover effects using `onMouseEnter` and `onMouseLeave`
26-
return (
27-
<View
28-
onClick={onPress}
29-
onMouseEnter={() => setIsHovered(true)}
30-
onMouseLeave={() => setIsHovered(false)}
31-
style={{ transitionDuration: '150ms', opacity: isHovered ? 0.5 : 1 }}
32-
{...props}
33-
{...rest}
34-
>
35-
<Text>{children}</Text>
36-
</View>
37-
);
38-
}
39-
4023
return (
41-
<TouchableOpacity onPress={onPress} {...props} {...rest}>
24+
<Pressable {...props} {...rest}>
4225
<Text>{children}</Text>
43-
</TouchableOpacity>
26+
</Pressable>
4427
);
4528
};
46-
47-
function Home() {
48-
return <LinkButton to={{ screen: 'Profile', params: { id: 'jane' } }}>Go to Jane's profile</LinkButton>;
49-
}
5029
```
5130

5231
Then you can use the `LinkButton` component elsewhere in your app:
5332

5433
```js
5534
function Home() {
56-
return <LinkButton to={{ screen: 'Profile', params: { id: 'jane' } }}>Go to Jane's profile</LinkButton>;
35+
return (
36+
<LinkButton screen="Profile" params={{ id: 'jane' }}>
37+
Go to Jane's profile
38+
</LinkButton>
39+
);
5740
}
5841
```
5942
60-
The `props` object returned by `useLinkProps` contains the required props for accessible link components. When we use these props on `View`, `Text` etc., the link component responds to user actions such as `Ctrl+Click`/`⌘+Click` to open links in new tab while keeping regular clicks within the same web page.
61-
62-
There are couple of important things to note when using `useLinkProps` with current version of React Native for Web:
63-
64-
1. You must explicitly pass `onPress` as the `onClick` prop, otherwise in-page navigation won't work
65-
2. You can only use `View` or `Text` with `useLinkProps`. The `TouchableX` components don't support a correct `onClick` event which we need
66-
67-
In a future version of React Native for Web, these won't be an issue and you'll be able to have the same code for links on Web, iOS and Android. But until then, you need to write platform specific code for Web and native.
68-
6943
## Options
7044
71-
### `to`
45+
### `screen` and `params`
7246
73-
You can pass an object with a `screen` property:
47+
You can pass `screen` and `params` to navigate to a screen on press:
7448
7549
```js
7650
function Home() {
7751
return (
78-
<LinkButton
79-
to={{ screen: 'Profile', params: { id: 'jane' } }}
80-
>
52+
<LinkButton screen="Profile" params={{ id: 'jane' }}>
8153
Go to Jane's profile
8254
</LinkButton>
8355
);
8456
}
8557
```
8658

87-
The syntax of this object is the same as [navigating to a screen in a nested navigators](nesting-navigators.md#navigating-to-a-screen-in-a-nested-navigator). This uses a `navigate` action for navigation by default, unless you specify a different action.
88-
89-
Alternatively, you can also pass an absolute path to the screen, e.g. - `/profile/jane`.
59+
If you want to navigate to a nested screen, you can pass the name of the `screen` in `params` similar to [navigating to a screen in a nested navigator](nesting-navigators.md#navigating-to-a-screen-in-a-nested-navigator):
9060

91-
This will be used for the `href` prop as well as for in-page navigation.
61+
```js
62+
<LinkButton screen="Root" params={{ screen: 'Post', params: { id: 123 } }}>
63+
Go to post 123
64+
</LinkButton>
65+
```
9266

9367
### `action`
9468

@@ -104,7 +78,8 @@ import { StackActions } from '@react-navigation/native';
10478
function Home() {
10579
return (
10680
<LinkButton
107-
to={{ screen: 'Profile', params: { id: 'jane' } }}
81+
screen="Profile"
82+
params={{ id: 'jane' }}
10883
action={StackActions.replace('Profile', { id: 'jane' })}
10984
>
11085
Go to Jane's profile
@@ -113,4 +88,23 @@ function Home() {
11388
}
11489
```
11590
116-
If the `action` prop is not specified, the path provided to the `to` prop will be used and dispatched as a `navigate` action.
91+
The `screen` and `params` props can be omitted if the `action` prop is specified. In that case, we recommend specifying the `href` prop as well to ensure that the link is accessible.
92+
93+
### `href`
94+
95+
The `href` is used for the `href` attribute of the anchor tag on the Web to make the links accessible. By default, this is automatically determined based on the [`linking` options](navigation-container.md#linking) using the `screen` and `params` props.
96+
97+
If you want to use a custom `href`, you can pass it as the `href` prop:
98+
99+
```js
100+
function Home() {
101+
return (
102+
<LinkButton
103+
action={StackActions.replace('Profile', { id: 'jane' })}
104+
href="/users/jane"
105+
>
106+
Getting Started
107+
</LinkButton>
108+
);
109+
}
110+
```

0 commit comments

Comments
 (0)