question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`useMatch` not able match more than one param

See original GitHub issue

Describe the bug If my URL contains more than one param like /menu/:menuId/sub/:subId, useMatch will only return menuId when the route is match. I also use useMatchRoute to make sure that the route is really matches.

To Reproduce Steps to reproduce the behavior:

  1. Create a route that contains two params or more, like /menu/:menuId/sub/:subId
  2. In the corresponding route element, call useMatch
  3. And observe the params that return by useMatch

Expected behavior The useMatch should return both menuId & subId

Screenshots

Desktop (please complete the following information):

  • OS: MacOS
  • Browser: Microsoft Edge
  • Version: 96.0.1054.43

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
chungweileong94commented, Dec 19, 2021

I personally find that the useMatches is quite difficult to use in scenarios like if I want to get all the match params. It would be great if we have something like const params = useParams().

As for now, I created a custom hook to merge all the match params from useMatches, it feels hacky, but works for me

export const useParams = <TGenerics extends PartialGenerics = DefaultGenerics>() => {
  const matches = useMatches<TGenerics>();
  const params = useMemo(
    () =>
      matches.reduce((result, match) => {
        return {...result, ...match.params};
      }, {} as TGenerics['Params']),
    [matches],
  );
  return params;
};
0reactions
chungweileong94commented, Dec 24, 2021

Why won’t useMatch().params not work? They are merged down the match tree to include parent params. Tanner Linsley On Dec 23, 2021, 9:50 AM -0700, Chung Wei Leong @.>, wrote: > Yeah, my point is that you don’t have to do that, if you setup your MakeGenerics like in the example I showed you. > Do you have MakeGenerics setup? You won’t need to merge them all together like that if you do. Hmm, I understand that, but from what I know MakeGenerics is just a type. The problem I was facing will happen on both JavaScript & TypeScript, setup the MakeGenerics won’t magically merge them all. For Example: export type LocationGenerics = MakeGenerics<{ Params: { postId: string; commentId: string; }; }>; const location = new ReactLocation<LocationGenerics>(); const routes: Route<LocationGenerics>[] = [ … {path: ‘/post/:postId/comment/:commentId’, element: <Page />}, {path: ‘/post/:postId’, element: <Page />}, … ]; … // Let say the route is “http:locahost:3000/post/1/comment/2” const matches = useMatches<LocationGenerics>(); The matches will be an array of all match results, that’s why I still have to search through the array to get the correct match that contains the params I want [ { // … params: {postId: 1}, }, { // … params: {postId: 1, commentId: 2}, }, ]; If I use useMatch, it will return { // … params: {postId: 1}, }, Which is definitely not something I want to use. — Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android. You are receiving this because you are subscribed to this thread.Message ID: @.>

Uh my bad, you are right, I’m actually confused by my current code setup. Yeah, it works as expected now👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

why is useMatch not working in React router? - Stack Overflow
I am trying to return the nearest current route match within the context of where it's called. I am quite new to react...
Read more >
useMatch does not match against query params #1460 - GitHub
When using a Link that does routing with a query params such as routes.home({filter: "DASHBOARD"}), this matches for all home routes and ...
Read more >
useMatch - Reach Router
useMatch. Matches a path to the location. Matching is relative to any parent Routers, but not parent match's, because they render even if...
Read more >
Match - React Router: Declarative Routing for React.js
A match object contains information about how a <Route path> matched the URL. match objects contain the following properties: params - (object) Key/value ......
Read more >
Use matchPath to Match Nested Route Paths in Parent Routes ...
In this lesson we'll use the `matchPath` function exported by react-router to find active nested routes inside of a parent component.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found