How to access data of parent loader?
My index route loader fetches some data. But I cannot use it inside child route.
indexRoute.useLoader() throws the following error error:
How can I access parent route loader result?
16 Replies
metropolitan-bronzeโข3y ago
From my experiments:
a) When using data inside a component being rendered by current route, you can retrieve it with
myRoute.getLoader(). Preferably myRoute.getLoader(myRoute.useRouteContext().loaderOptions).
b) When using data inside a component being rendered by a subroute, you cannot access the data directly from the loader itself with myRoute().getLoader(). You just get an error. However, you can access the data using useLoaderInstance({key: 'myLoaderKey'}).blank-aquamarineโข3y ago
You can use the useLoaderInstance if you are also using a Loader constructor
If you just want to reference a parent loader, I believe you can import the useLoader hook from "tanstack/react-router" and then use it like this
foreign-sapphireโข2y ago
This doesn't work for me, same error
absent-sapphireโข2y ago
please provide a minimal complete example, e.g. by forking on of the existing examples on stackblitz
xenial-blackโข2y ago
I get the same
Invariant Failed error. Here is a fork with the replicated error:
https://stackblitz.com/edit/tanstack-router-1gyeu8?file=src%2Froutes%2Fposts.index.tsx
Navigate to the Posts tab in the preview.Joakim Strandell
StackBlitz
Router Basic React Query File Based Example (forked) - StackBlitz
Run official live example code for Router Basic React Query File Based, created by Tanstack on StackBlitz
national-goldโข2y ago
not an expert but the error goes away for me if I add a trailing slash:
after all, your route is defined as:
but yeah there seems to be a
/posts/ and a /posts route ๐ค
also, since you are using query - why not get access with useQuery ?absent-sapphireโข2y ago
@TkDodo ๐ฎ is absolutely right, if you are inside of
/posts/, then use this as from
however, the loader seems to be in /posts
so that's the parent
you could use the useParentMatches hook to access data from the parentnational-goldโข2y ago
so
posts.tsx creates /posts but posts.index.tsx creates /posts/ ?absent-sapphireโข2y ago
yes
if you don't have any children routes of
/posts, you don't need the index route
but if you have, and you want to render something at /posts/without any further path segments, you need the index routenational-goldโข2y ago
and
posts.tsx is always rendered, like route.tsx inside a posts directory?
k, sorry for the confusion, I've only worked with nested directories until nowabsent-sapphireโข2y ago
posts.tsx is at the same level as posts directory.
the same thing is route.tsx insides of posts directory
or
xenial-blackโข2y ago
I prefer using the data from the loader as it is not possibly undefined.
absent-sapphireโข2y ago
how about
useSuspenseQuery?xenial-blackโข2y ago
I must have missed that hook. Will try that out. Thanks.
But regarding the Invariant Failed error. Is useLoaderData not the way to load parent loader data?
absent-sapphireโข2y ago
no it is not
absent-sapphireโข2y ago
FYI , we released https://github.com/TanStack/router/releases/tag/v1.22.3
now you can access the parent loader data
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...