Skip to content
Source
Sign inSign up
TanStack/router

Match Error with useSearch inside a route nested in a layout and pathless route group #2501

Send to my agent
ClosedHazihell(GitHub profile, opens in a new tab) opened this issue 6 comments

Which project does this relate to?

Router

Describe the bug

Using a layout inside a pathless route group directory breaks useSearch. After calling the hook inside the route or the layout, with the correct from value, it gives the error: Invariant failed: Could not find an active match from '/_layout/about'.

I also noticed that the from property type is inferred as __layout/about_, which isn't good for consistency, as the from property in the Link component is inferred as /about.

Your Example Website or App

https://stackblitz.com/edit/github-qwwxnn?file=src%2Froutes%2F(test)%2F_layout.about.tsx

Steps to Reproduce the Bug or Issue

  1. Go to https://stackblitz.com/edit/github-qwwxnn?file=src%2Froutes%2F(test)%2F_layout.about.tsx
  2. Click on About Link.
  3. See the error on the page.

Expected behavior

The useSearch hook should infer the same from types as the Link component, and it should not throw an error.

Screenshots or Videos

Image

Platform

  • OS: [Windows]
  • Browser: [Chrome]
  • Version: [^1.64.0]

Additional context

No response

  1. SeanCassiere added labelsbug

  2. Runtime routeId is incorrect.

    Image

  3. I have the same error when using useLoaderData, useMatch.

  4. route groups are now not removed from the route id anymore to be consistent between types and runtime.

  5. @schiller-manuel , thanks a lot for the quick fix!

    For consistency, can’t we have the same values inferred in the from prop of the Link component? Using '/(test)/_layout/about' instead of just '/about' in useSearch feels odd.

    Image

  6. while both are named from, they referenced different concepts. from in useSearch is a route id

    whereas from in <LInk> is a route path

    btw, you can also use Route.useSearch() so you don't have to type this out.

  7. while both are named from, they referenced different concepts. from in useSearch is a route id

    whereas from in <LInk> is a route path

    btw, you can also use Route.useSearch() so you don't have to type this out.

    I'm familiar with Route.useSearch(), but I usually need it outside the route file, and managing its exports/imports isn't ideal because they're default and have generic route names. I'm also trying to avoid circular dependencies, if that makes sense.

    Thanks for taking time to answer this.

Sign in to join this conversation.