Skip to content

Commit 256ae25

Browse files
committedDec 3, 2024
Document path alias in linking
1 parent 7520267 commit 256ae25

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed
 

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

+58-1
Original file line numberDiff line numberDiff line change
@@ -1248,7 +1248,7 @@ Depending on your requirements, you can use this functionality to parse and stri
12481248
12491249
## Matching regular expressions
12501250
1251-
If you need more complex matching logic, you can use regular expressions to match the path. For example, if you want to use the pattern `@username` to match a user's profile, you can use a regular expression to match the path.
1251+
If you need more complex matching logic, you can use regular expressions to match the path. For example, if you want to use the pattern `@username` to match a user's profile, you can use a regular expression to match the path. This allows you to have the same path pattern for multiple screens, but fine-tune the matching logic to be more specific for a particular screen.
12521252
12531253
Regular expressions can be specified between parentheses `(` and `)` in the after a param name. For example:
12541254
@@ -1258,6 +1258,12 @@ Regular expressions can be specified between parentheses `(` and `)` in the afte
12581258
```js
12591259
const RootStack = createStackNavigator({
12601260
screens: {
1261+
Feed: {
1262+
screen: FeedScreen,
1263+
linking: {
1264+
path: ':sort(latest|popular)',
1265+
},
1266+
},
12611267
Profile: {
12621268
screen: ProfileScreen,
12631269
linking: {
@@ -1274,6 +1280,7 @@ const RootStack = createStackNavigator({
12741280
```js
12751281
const config = {
12761282
screens: {
1283+
Feed: ':sort(latest|popular)',
12771284
Profile: ':username(@[A-Za-z0-9_]+)',
12781285
},
12791286
};
@@ -1292,6 +1299,56 @@ Regular expressions are an advanced feature. They cannot be validated to warn yo
12921299
12931300
:::
12941301
1302+
## Alias for paths
1303+
1304+
If you want to have multiple paths for the same screen, you can use the `alias` property to specify an array of paths. This can be useful to keep backward compatibility with old URLs while transitioning to a new URL structure.
1305+
1306+
For example, if you want to match both `/users/:id` and `/:id` to the `Profile` screen, you can do this:
1307+
1308+
<Tabs groupId="config" queryString="config">
1309+
<TabItem value="static" label="Static" default>
1310+
1311+
```js
1312+
const RootStack = createStackNavigator({
1313+
screens: {
1314+
Profile: {
1315+
screen: ProfileScreen,
1316+
linking: {
1317+
path: ':id',
1318+
alias: ['users/:id'],
1319+
},
1320+
},
1321+
},
1322+
});
1323+
```
1324+
1325+
</TabItem>
1326+
<TabItem value="dynamic" label="Dynamic">
1327+
1328+
```js
1329+
const config = {
1330+
screens: {
1331+
Profile: {
1332+
path: ':id',
1333+
alias: ['users/:id'],
1334+
},
1335+
},
1336+
};
1337+
```
1338+
1339+
</TabItem>
1340+
</Tabs>
1341+
1342+
In this case, when the URL is `/users/jane` or `/jane`, it'll match the `Profile` screen. The `path` is the primary pattern that will be used to generate the URL, e.g. when navigating to the `Profile` screen in the app on the Web. The patterns in `alias` will be ignored when generating URLs. The `alias` patterns are not used for matching any child screens in nested navigators.
1343+
1344+
On the web, if a screen containing an alias contains a nested navigator, the URL matching the alias will only be used to match the screen, and will be updated to the URL of the focused child screen once the app renders.
1345+
1346+
Each item in the `alias` array can be a string matching the syntax of the `path` property, or an object with the following properties:
1347+
1348+
- `path` (required) - The path pattern to match.
1349+
- `exact` - Whether to match the path exactly. Defaults to `false`. See [Matching exact paths](#matching-exact-paths) for more details.
1350+
- `parse` - Function to parse path segments into param values. See [Passing params](#passing-params) for more details.
1351+
12951352
## Advanced cases
12961353
12971354
For some advanced cases, specifying the mapping may not be sufficient. To handle such cases, you can specify a custom function to parse the URL into a state object ([`getStateFromPath`](navigation-container.md#linkinggetstatefrompath)), and a custom function to serialize the state object into an URL ([`getPathFromState`](navigation-container.md#linkinggetpathfromstate)).

0 commit comments

Comments
 (0)