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-7.x/configuring-links.md
+58-1
Original file line number
Diff line number
Diff line change
@@ -1248,7 +1248,7 @@ Depending on your requirements, you can use this functionality to parse and stri
1248
1248
1249
1249
## Matching regular expressions
1250
1250
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.
1252
1252
1253
1253
Regular expressions can be specified between parentheses `(` and `)` in the after a param name. For example:
1254
1254
@@ -1258,6 +1258,12 @@ Regular expressions can be specified between parentheses `(` and `)` in the afte
@@ -1292,6 +1299,56 @@ Regular expressions are an advanced feature. They cannot be validated to warn yo
1292
1299
1293
1300
:::
1294
1301
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
+
constRootStack=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
+
constconfig= {
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
+
1295
1352
## Advanced cases
1296
1353
1297
1354
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