T
TanStackโ€ข2y ago
other-emerald

Optimum way to use route context in a component?

Hi everyone, This may be quite a basic question (!) I am wondering the best way to use context that is provided at top level RouterProvider, in a component that is outside a page/component located in the routes folder? e.g. index.tsx inside routes imports component.tsx from src/mycomponents - how should component.tsx access context? when I tried it like this:
const { authStatus } = useRouteContext({
from: "/",
})
const { authStatus } = useRouteContext({
from: "/",
})
I got error:
Invariant failed: useMatch("/") is being called in a component that is meant to render the '__root__' route. Did you mean to 'useMatch("/", { strict: false })' or 'useRoute("/")' instead?
Invariant failed: useMatch("/") is being called in a component that is meant to render the '__root__' route. Did you mean to 'useMatch("/", { strict: false })' or 'useRoute("/")' instead?
Thank you very much
25 Replies
rival-black
rival-blackโ€ข2y ago
for now you can exactly do what the error says:
useRouteContext("/", { strict: false })
useRouteContext("/", { strict: false })
other-emerald
other-emeraldOPโ€ข2y ago
Thanks for the quick reply! May I ask was I doing the correct thing and is the strict to false a bad practice? (hoping to learn understand better)
rival-black
rival-blackโ€ข2y ago
not bad practice. currently, strict is... TOO strict
other-emerald
other-emeraldOPโ€ข2y ago
like 70s public school strict !
rival-black
rival-blackโ€ข2y ago
at least ๐Ÿ˜„ we are planning to loosen it up a few decades
other-emerald
other-emeraldOPโ€ข2y ago
The error was a bit confusing with "useMatch"
rival-black
rival-blackโ€ข2y ago
yeah that's the underlying primitive hook that useRouteContext uses
other-emerald
other-emeraldOPโ€ข2y ago
Gotcha. Cool thank you very much, Manuel Also I should still be using "from", rather than just "/" ?
rival-black
rival-blackโ€ข2y ago
no i was typing nonsense
useMatch({from: '/', strict: false})
useMatch({from: '/', strict: false})
other-emerald
other-emeraldOPโ€ข2y ago
useRouteContext rather?
rival-black
rival-blackโ€ข2y ago
yeah of course
other-emerald
other-emeraldOPโ€ข2y ago
I get TS errors
const { authStatus } = useRouteContext({
from: "/",
strict: false,
})
const { authStatus } = useRouteContext({
from: "/",
strict: false,
})
It errors out when assiging false value, but accepts true value
Types of property 'strict' are incompatible.
Type 'false' is not assignable to type 'true'.
Types of property 'strict' are incompatible.
Type 'false' is not assignable to type 'true'.
rival-black
rival-blackโ€ข2y ago
yeah sorry... discord needs to embed a VS code instance ๐Ÿ˜„ so I cannot type invalid TS to begin with if strict: false is specified, you cannot supply a from
useRouteContext({
strict: false,
})
useRouteContext({
strict: false,
})
other-emerald
other-emeraldOPโ€ข2y ago
Oh okay, as the docs say the from is required, but I am guessing that strict false removes that requirement
rival-black
rival-blackโ€ข2y ago
the from is just used to narrow then typing where does it say so?
other-emerald
other-emeraldOPโ€ข2y ago
useRouteContext hook | TanStack Router Docs
The useRouteContext method is a hook that returns the current context for the current route. This hook is useful for accessing the current route context in a component. useRouteContext options
other-emerald
other-emeraldOPโ€ข2y ago
No description
rival-black
rival-blackโ€ข2y ago
hm yes, needs to be fixed
rival-black
rival-blackโ€ข2y ago
useMatch hook | TanStack Router Docs
The useMatch hook returns the closest RouteMatch in the component tree. The raw route match contains all of the information about a route match in the router and also powers many other hooks under the hood like useParams, useLoaderData, useRouteContext, and useSearch. useMatch options
other-emerald
other-emeraldOPโ€ข2y ago
Aha ๐Ÿ™‚ we have figured it out! nice one thanks again Working now. I guess the "disadvantage" of this is the loss of type safety as the useMatch doc describes
rival-black
rival-blackโ€ข2y ago
yes however we are working on allowing a from that is a parent of your current route
other-emerald
other-emeraldOPโ€ข2y ago
Gotcha, which would allow the root route then Nice ๐Ÿ™‚
rival-black
rival-blackโ€ข2y ago
GitHub
Release v1.22.3 ยท TanStack/router
Version 1.22.3 - 3/22/2024, 10:55 PM Changes Fix do not use CatchBoundary if neither route nor router default error component is configured (#1355) (c5c325d) by Manuel Schiller loosen strict check...
rival-black
rival-blackโ€ข2y ago
in your case you would use
import {
useRouteContext,
rootRouteId,
} from '@tanstack/react-router'

const { authStatus } = useRouteContext({
from: rooteRouteId
})
import {
useRouteContext,
rootRouteId,
} from '@tanstack/react-router'

const { authStatus } = useRouteContext({
from: rooteRouteId
})
other-emerald
other-emeraldOPโ€ข2y ago
Hah amazing, what a turnaround time! well done! working ๐Ÿ˜„

Did you find this page helpful?