Skip to content

Commit 6596168

Browse files
committed
Improve docs for configuring links
1 parent e2879f0 commit 6596168

File tree

1 file changed

+104
-27
lines changed

1 file changed

+104
-27
lines changed

versioned_docs/version-5.x/configuring-links.md

+104-27
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,39 @@ In this guide, we will configure React Navigation to handle external links. This
1010
2. Enable URL integration in browser when using on web
1111
3. Use [`<Link />`](link.md) or [`useLinkTo`](use-link-to.md) to navigate using paths.
1212

13+
Make sure that you have [configured deep links](deep-links.md) in your app before proceeding. If you have an Android or iOS app, remember to specify the [`prefixes`](navigation-container.md#linkingprefixes) option.
14+
15+
The `NavigationContainer` accepts a [`linking`](navigation-container.md#linking) prop that makes it easier to handle incoming links. The 2 of the most important properties you can specify in the `linking` prop are `prefixes` and `config`:
16+
17+
```js
18+
import { NavigationContainer } from '@react-navigation/native';
19+
20+
const linking = {
21+
prefixes: [
22+
/* your linking prefixes */
23+
],
24+
config: {
25+
/* configuration for matching screens with paths */
26+
},
27+
};
28+
29+
function App() {
30+
return (
31+
<NavigationContainer linking={linking} fallback={<Text>Loading...</Text>}>
32+
{/* content */}
33+
</NavigationContainer>
34+
);
35+
}
36+
```
37+
38+
When you specify the `linking` prop, React Navigation will handling incoming links automatically. On Android and iOS, it'll use React Native's [`Linking` module](https://reactnative.dev/docs/linking) to handle incoming links, both when the app was opened with the link, and when new links are received when the app is open. On the Web, it'll use the [History API](https://developer.mozilla.org/en-US/docs/Web/API/History_API) to sync the URL with the browser.
39+
40+
> Note: Currently there seems to be bug ([facebook/react-native#25675](https://github.com/facebook/react-native/issues/25675)) which results in it never resolving on Android. We add a timeout to avoid getting stuck forever, but it means that the link might not be handled in some cases.
41+
42+
You can also pass a [`fallback`](navigation-container.md#fallback) prop to `NavigationContainer` which controls what's displayed when React Navigation is trying to resolve the initial deep link URL.
43+
44+
## Mapping path to route names
45+
1346
To handle a link, you need to translate it to a valid navigation state and vice versa. For example, the path `/rooms/chat?user=jane` may be translated to a state object like this:
1447

1548
```js
@@ -27,18 +60,54 @@ const state = {
2760
},
2861
},
2962
],
63+
};
64+
```
65+
66+
By default, React Navigation will use the path segments as the route name when parsing the URL. But directly translating path segments to route names may not be the expected behavior, and your.
67+
68+
For example, you might want to parse the path `/feed/latest` to something like:
69+
70+
```js
71+
const state = {
72+
routes: [
73+
{
74+
name: 'Chat',
75+
params: {
76+
sort: 'latest',
77+
},
78+
},
79+
];
3080
}
3181
```
3282

33-
The `NavigationContainer` accepts a `linking` prop that makes it easier to handle incoming links. React
83+
You can specify the [`config`](navigation-container.md#linkingconfig) option in `linking` to control how the deep link is parsed to suit your needs.
84+
85+
```js
86+
const config = {
87+
screens: {
88+
Chat: 'feed/:sort',
89+
Profile: 'user',
90+
},
91+
};
92+
```
93+
94+
Here `Chat` is the name of the screen that handles the URL `/feed`, and `Profile` handles the URL `/user`.
3495

35-
## Prefix
96+
The config option can then be passed in the `linking` prop to the container:
3697

3798
```js
3899
import { NavigationContainer } from '@react-navigation/native';
39100

101+
const config = {
102+
screens: {
103+
Chat: 'feed/:sort',
104+
Profile: 'user',
105+
},
106+
};
107+
40108
const linking = {
41109
prefixes: ['https://mychat.com', 'mychat://'],
110+
config,
42111
};
43112

44113
function App() {
@@ -50,46 +119,54 @@ function App() {
50119
}
51120
```
52121

53-
The `prefixes` option is needed to match the incoming deep links and strip the prefix before React Navigation parses them. It's not needed for web integration.
122+
The config object must match the navigation structure for your app. For example, the above configuration is if you have `Chat` and `Profile` screens in the navigator at the root:
54123

55-
The `fallback` prop controls what's displayed when React Navigation is trying to resolve the initial deep link URL.
56-
57-
> Note: By default, deep link integration uses React Native's `Linking.getInitialUrl()` under the hood. Currently there seems to be bug ([facebook/react-native#25675](https://github.com/facebook/react-native/issues/25675)) which results in it never resolving on Android. We add a timeout to avoid getting stuck forever, but it means that the link might not be handled in some cases.
58-
59-
## Mapping path to route names
60-
61-
By default, React Navigation will use the path segments as the route name when parsing the URL. But directly translating path segments to route names may not be the expected behavior, and your.
124+
```js
125+
function App() {
126+
return (
127+
<Stack.Navigator>
128+
<Stack.Screen name="Chat" component={ChatScreen} />
129+
<Stack.Screen name="Profile" component={ProfileScreen} />
130+
</Stack.Navigator>
131+
);
132+
}
133+
```
62134

63-
For example, you might want to parse the path `/feed/latest` to something like:
135+
If your `Chat` screen is inside a nested navigator, we'd need to account for that. For example, consider the following structure where your `Profile` screen is at the root, but the `Chat` screen is nested inside `Home`:
64136

65137
```js
66-
const state = {
67-
routes: [
68-
{
69-
name: 'Chat',
70-
params: {
71-
sort: 'latest',
72-
},
73-
},
74-
];
138+
function App() {
139+
return (
140+
<Stack.Navigator>
141+
<Stack.Screen name="Home" component={HomeScreen} />
142+
<Stack.Screen name="Profile" component={ProfileScreen} />
143+
</Stack.Navigator>
144+
);
145+
}
146+
147+
function HomeScreen() {
148+
return (
149+
<Tab.Navigator>
150+
<Tab.Screen name="Chat" component={ChatScreen} />
151+
</Tab.Navigator>
152+
);
75153
}
76154
```
77155

78-
You can specify a separate `config` option to control how the deep link is parsed to suit your needs.
156+
For above structure, our configuration will look like this:
79157

80158
```js
81-
const linking = {
82-
prefixes: ['https://mychat.com', 'mychat://'],
83-
config: {
84-
screens: {
159+
const config = {
160+
screens: {
161+
Home: {
85162
Chat: 'feed/:sort',
86-
Profile: 'user',
87163
},
164+
Profile: 'user',
88165
},
89166
};
90167
```
91168

92-
Here `Chat` is the name of the screen that handles the URL `/feed`, and `Profile` handles the URL `/user`.
169+
Similarly, any nesting needs to be reflected in the configuration. See [handling nested navigators](#handling-nested-navigators) for more details.
93170

94171
## Passing params
95172

0 commit comments

Comments
 (0)