-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make it possible to specify route param values that need to match. If this is your route: `<Route path="/blog/{year}/{month}/{day}" page={BlogPostPage} name="blogPost" />` And you want to only match posts from 2001 you can now do this: `useMatch('/blog/{year}/{month}/{day}', { routeParams: { year: '2001' } })` This is **finally** a solution to matching route paths. The work started in #7469, but we were never able to come up with an api/dx that we really liked. This PR and #9755 together however provides a solution that we're much more happy with, and that also supports the use case outlined in that original PR. Here's the example from #7469 as it could be solved with the code in this PR ```jsx const Navbar () => { const { project } = useParams() const routePaths = useRoutePaths() const modes = [ { name: "Info", route: routes.info({ project }), match: useMatch(routePaths.info), // using the hook together with routePaths }, { name: "Compare", route: routes.compare({ project, id: "1" }), match: useMatch(useRoutePath('compare')), // alternative to the above }, // ... ] return ( <> {modes.map((x) => <Button as={Link} to={x.route} isActive={x.match} />)} </> ) } ``` And, as described at the top of this description, we can also be more specific than in that example if needed. Like if we only wanted to match a specific project on the "Compare" route we could do this: ```jsx const modes = [ { name: "Info", route: routes.info({ project }), match: useMatch(routePaths.info), }, { name: "Compare against Alpha", route: routes.compare({ project, id: "1" }), match: useMatch(useRoutePath('compare'), { routeParams: { project: 'alpha' } }), }, { name: "Compare against Beta", route: routes.compare({ project, id: "1" }), match: useMatch(useRoutePath('compare'), { routeParams: { project: 'beta' } }), }, // ... ] ``` Here's another example ```jsx <Route path="/{dynamic}/{path}" page={ExamplePage} name="example" /> const exampleRoutePath = useRoutePath('example') // => '/{dynamic}/{path}' const matchOnlyDog = useMatch(exampleRoutePath, { routeParams: { dynamic: 'dog' }}) const matchFullyDynamic = useMatch(exampleRoutePath) ``` In the above example, if the current page url was `https://example.org/dog/fido` then both `matchOnlyDog` and `matchFullyDynamic` would have `match: true`. If the current page instead was `https://example.org/cat/garfield` then only `matchFullyDynamic` would match (This PR replaces #9774)
- Loading branch information
Showing
3 changed files
with
294 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters