Dynamic Routes
Edit this pageWhen a path is unknown ahead of time, it can be treated as a flexible parameter that is passed on to the component:
The colon (:
) indicates that id
can be any string.
Once a URL matches the pattern, the User
component will be shown.
When using dynamic segments, the values can be accessed via the useParams
hook within the component.
Routes that share the same path match will be treated as the same route.
If you want to force re-render you can wrap your component in a keyed Show
like:
Each path parameter can be validated using a MatchFilter
.
This allows for more complex routing descriptions rather than just checking the presence of a parameter.
Here, matchFilters
prop allows for validation of the parent
, id
and withHtmlExtension
parameters against the filters defined in filters
.
If the validation fails, the route will not match.
So in this example:
/users/mom/123/contact.html
would match,/users/dad/123/about.html
would match,/users/aunt/123/contact.html
would not match as:parent
is not 'mom' or 'dad',/users/mom/me/contact.html
would not match as:id
is not a number,/users/dad/123/contact
would not match as:withHtmlExtension
is missing.html
.
Optional Parameters
Parameters can be specified as optional by adding a question mark to the end of the parameter name:
Wildcard Routes
:param
provides an arbitrary name at that point in the path.
Using an asterisk (*
) will provide a way to match any end of the path:
If the wild part of the path to the component as a parameter needs to be exposed, it can be named:
Note: that the wildcard token must be the last part of the path; foo/*any/bar
will not create any routes.
Multiple Paths
Routes also support defining multiple paths using an array. This allows a route to remain mounted and not rerender when switching between two or more locations that it matches: