Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider exposing matchRoute helper function #455

Closed
acelaya opened this issue Jun 13, 2024 · 3 comments · Fixed by #456
Closed

Consider exposing matchRoute helper function #455

acelaya opened this issue Jun 13, 2024 · 3 comments · Fixed by #456

Comments

@acelaya
Copy link
Contributor

acelaya commented Jun 13, 2024

I wanted to ask if you would consider exposing the matchRoute helper function that is internally used by useRoute and Switch.

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.

@molefrog
Copy link
Owner

I wanted to ask if you would consider exposing the matchRoute helper function that is internally used by useRoute and Switch.

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.

@acelaya
Copy link
Contributor Author

acelaya commented Jun 17, 2024

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 #456

@molefrog
Copy link
Owner

Just published the new version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants