You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: versioned_docs/version-5.x/configuring-links.md
+104-27
Original file line number
Diff line number
Diff line change
@@ -10,6 +10,39 @@ In this guide, we will configure React Navigation to handle external links. This
10
10
2. Enable URL integration in browser when using on web
11
11
3. Use [`<Link />`](link.md) or [`useLinkTo`](use-link-to.md) to navigate using paths.
12
12
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`:
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
+
13
46
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:
14
47
15
48
```js
@@ -27,18 +60,54 @@ const state = {
27
60
},
28
61
},
29
62
],
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
+
conststate= {
72
+
routes: [
73
+
{
74
+
name:'Chat',
75
+
params: {
76
+
sort:'latest',
77
+
},
78
+
},
79
+
];
30
80
}
31
81
```
32
82
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
+
constconfig= {
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`.
34
95
35
-
## Prefix
96
+
The config option can then be passed in the `linking` prop to the container:
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:
54
123
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.
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`:
0 commit comments