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:
I got error:
Thank you very much
25 Replies
rival-blackโข2y ago
for now you can exactly do what the error says:
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โข2y ago
not bad practice.
currently, strict is... TOO strict
other-emeraldOPโข2y ago
like 70s public school strict !
rival-blackโข2y ago
at least ๐
we are planning to loosen it up a few decades
other-emeraldOPโข2y ago
The error was a bit confusing with "useMatch"
rival-blackโข2y ago
yeah that's the underlying primitive hook that useRouteContext uses
other-emeraldOPโข2y ago
Gotcha. Cool thank you very much, Manuel
Also I should still be using "from", rather than just "/" ?
rival-blackโข2y ago
no i was typing nonsense
other-emeraldOPโข2y ago
useRouteContext rather?
rival-blackโข2y ago
yeah
of course
other-emeraldOPโข2y ago
I get TS errors
It errors out when assiging false value, but accepts true value
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
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โข2y ago
the
from is just used to narrow then typing
where does it say so?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-emeraldOPโข2y ago

rival-blackโข2y ago
hm yes, needs to be fixed
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-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โข2y ago
yes
however we are working on allowing a
from that is a parent of your current routeother-emeraldOPโข2y ago
Gotcha, which would allow the root route then
Nice ๐
rival-blackโข2y ago
FYI , we released https://github.com/TanStack/router/releases/tag/v1.22.3
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โข2y ago
in your case you would use
other-emeraldOPโข2y ago
Hah amazing, what a turnaround time! well done!
working ๐