Skip to content

Commit 15a66bf

Browse files
committed
Document automatic linking
1 parent 9bcaa5d commit 15a66bf

File tree

3 files changed

+79
-8
lines changed

3 files changed

+79
-8
lines changed

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

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ title: Configuring links
44
sidebar_label: Configuring links
55
---
66

7+
import Tabs from '@theme/Tabs';
8+
import TabItem from '@theme/TabItem';
9+
710
In this guide, we will configure React Navigation to handle external links. This is necessary if you want to:
811

912
1. Handle deep links in React Native apps on Android and iOS
@@ -12,11 +15,45 @@ In this guide, we will configure React Navigation to handle external links. This
1215

1316
Make sure that you have [configured deep links](deep-linking.md) in your app before proceeding. If you have an Android or iOS app, remember to specify the [`prefixes`](navigation-container.md#linkingprefixes) option.
1417

18+
<Tabs groupId="config" queryString="config">
19+
<TabItem value="static" label="Static" default>
20+
21+
The [`Navigation`](static-configuration.md#createstaticnavigation) component accepts a [`linking`](static-configuration.md#differences-in-the-linking-prop) prop that makes it easier to handle incoming links:
22+
23+
```js
24+
import { createStaticNavigation } from '@react-navigation/native';
25+
26+
// highlight-start
27+
const linking = {
28+
enabled: 'auto' /* Automatically generate paths for all screens */,
29+
prefixes: [
30+
/* your linking prefixes */
31+
],
32+
};
33+
// highlight-end
34+
35+
function App() {
36+
return (
37+
<Navigation
38+
// highlight-next-line
39+
linking={linking}
40+
fallback={<Text>Loading...</Text>}
41+
/>
42+
);
43+
}
44+
45+
const Navigation = createStaticNavigation(RootStack);
46+
```
47+
48+
</TabItem>
49+
<TabItem value="dynamic" label="Dynamic">
50+
1551
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`:
1652

1753
```js
1854
import { NavigationContainer } from '@react-navigation/native';
1955

56+
// highlight-start
2057
const linking = {
2158
prefixes: [
2259
/* your linking prefixes */
@@ -25,16 +62,24 @@ const linking = {
2562
/* configuration for matching screens with paths */
2663
},
2764
};
65+
// highlight-end
2866

2967
function App() {
3068
return (
31-
<NavigationContainer linking={linking} fallback={<Text>Loading...</Text>}>
69+
<NavigationContainer
70+
// highlight-next-line
71+
linking={linking}
72+
fallback={<Text>Loading...</Text>}
73+
>
3274
{/* content */}
3375
</NavigationContainer>
3476
);
3577
}
3678
```
3779

80+
</TabItem>
81+
</Tabs>
82+
3883
When you specify the `linking` prop, React Navigation will handle 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.
3984

4085
:::warning
@@ -43,7 +88,7 @@ Currently there seems to be bug ([facebook/react-native#25675](https://github.co
4388

4489
:::
4590

46-
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.
91+
You can also pass a [`fallback`](navigation-container.md#fallback) prop that controls what's displayed when React Navigation is trying to resolve the initial deep link URL.
4792

4893
## Prefixes
4994

@@ -69,7 +114,7 @@ const linking = {
69114
};
70115
```
71116

72-
### Filtering certain paths
117+
## Filtering certain paths
73118

74119
Sometimes we may not want to handle all incoming links. For example, we may want to filter out links meant for authentication (e.g. `expo-auth-session`) or other purposes instead of navigating to a specific screen.
75120

@@ -85,7 +130,7 @@ const linking = {
85130

86131
This is not supported on Web as we always need to handle the URL of the page.
87132

88-
### Apps under subpaths
133+
## Apps under subpaths
89134

90135
If your app is hosted under a subpath, you can specify the subpath at the top-level of the `config`. For example, if your app is hosted at `https://mychat.com/app`, you can specify the `path` as `app`:
91136

versioned_docs/version-7.x/static-configuration.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,35 @@ function App() {
308308
}
309309
```
310310

311-
This component is a wrapper around the `NavigationContainer` component and accepts the [same props and ref as the `NavigationContainer`](navigation-container.md) component. There's however one difference - the `linking` prop accepted by this component doesn't take a `config` property. Instead, the linking config is automatically inferred from the static config.
311+
This component is a wrapper around the `NavigationContainer` component and accepts the [same props and ref as the `NavigationContainer`](navigation-container.md) component. It is intended to be rendered once at the root of your app similar to how you'd use `NavigationContainer` component.
312312

313-
This is intended to be rendered once at the root of your app similar to how you'd use `NavigationContainer` component.
313+
### Differences in the `linking` prop
314+
315+
Similar to `NavigationContainer`, the component returned by `createStaticNavigation` also accepts a [`linking`](navigation-container.md#linking) prop. However, there are some key differences:
316+
317+
1. It's not possible to pass a full `config` object to the `linking` prop. It can only accept [`path`](configuring-links.md#apps-under-subpaths) and an [`initialRouteName` for the root navigator](configuring-links.md#rendering-an-initial-route).
318+
2. The linking config is collected from the [`linking`](#linking) properties specified in the screen configuration.
319+
3. It's possible to pass `enabled: 'auto'` to automatically generate paths for all leaf screens:
320+
321+
```js
322+
const Navigation = createStaticNavigation(RootStack);
323+
324+
const linking = {
325+
enabled: 'auto',
326+
prefixes: ['https://example.com', 'example://'],
327+
};
328+
329+
function App() {
330+
return <Navigation linking={linking} />;
331+
}
332+
```
333+
334+
Automatic path generation works as follows:
335+
336+
- Screens with an explicit `linking` property are not used for path generation and will be added as-is.
337+
- Screen names will be converted from `PascalCase` to `kebab-case` to use as the path (e.g. `NewsFeed` -> `news-feed`).
338+
- Unless a screen has explicit empty path (`path: ''`) to use for the homepage, the first leaf screen encountered will be used as the homepage.
339+
- Path generation only handles leaf screens, i.e. no path is generated for screens containing nested navigators. It's still possible to specify a path for them with an explicit `linking` property.
314340

315341
## `createComponentForStaticNavigation`
316342

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,8 +457,8 @@ const MyStack = createNativeStackNavigator({
457457

458458
The static configuration API provides the following benefits:
459459

460-
- Simpler type-checking with TypeScript, it's not necessary to specify screens and their params separately.
461-
- Easier deep linking setup, the linking configuration can be defined next to the screen.
460+
- **Simpler type-checking with TypeScript**: It's not necessary to specify screens and their params separately. See [Type checking with TypeScript](typescript.md?config=static) for more details.
461+
- **Easier deep linking setup**: Paths can be generated automatically. Linking configuration can be defined next to the screen for explicit configuration. See [Configuring links](configuring-links.md?config=static) for more details.
462462

463463
It's also possible to mix the static and dynamic configuration APIs. For example, you can use the static configuration API for the top-level navigators and the dynamic configuration API for the nested navigators where you need more flexibility.
464464

0 commit comments

Comments
 (0)