Closed acelaya closed 1 month ago
I wanted to ask if you would consider exposing the
matchRoute
helper function that is internally used byuseRoute
andSwitch
.I'm trying to create a custom hook which receives a list of routes in order to check which one matches, but this list of routes is dynamic, so it's not possible to call
useRoute
a bunch of times, since it needs to loop over the list of routes.However, if
matchRoute
was exported by this package, it would allow userland programmatic logic like this:import { matchRoute, useLocation, useRouter } from 'wouter-preact'; function useMatchingRoute(candidates: string[]): [string, Record] | undefined { const { parser } = useRouter(); const [location] = useLocation(); return useMemo(() => { for (const route of candidates) { const [matches, params] = matchRoute(parser, route, location); if (matches) { return [route, params]; } } return undefined; }, [location, parser, candidates]); }
I can provide a PR if you are ok with this.
Hey @acelaya, thank you for suggesting this. I think it makes a lot of sense, so feel free to open a pull request.
Hey @acelaya, thank you for suggesting this. I think it makes a lot of sense, so feel free to open a pull request.
Just created it https://github.com/molefrog/wouter/pull/456
Just published the new version.
I wanted to ask if you would consider exposing the
matchRoute
helper function that is internally used byuseRoute
andSwitch
.I'm trying to create a custom hook which receives a list of routes in order to check which one matches, but this list of routes is dynamic, so it's not possible to call
useRoute
a bunch of times, since it needs to loop over the list of routes.However, if
matchRoute
was exported by this package, it would allow userland programmatic logic like this:I can provide a PR if you are ok with this.