T
TanStack4mo ago
other-emerald

Hi all,

Is there a way to access the data loaded from parent/grandparent routes in a child page's loader method? My case is user is loaded at grandparent route, then projects at parent, then docs at child. In order to fetch the doc I need the user id and project id to do so, and this seems fairly difficult to get. I'm currently going down the router context path on sticking my user onto the context, but this feels unintuitive, as I would feel like all the data loaded in the chain should be available to its children.
2 Replies
rival-black
rival-black4mo ago
i don't understand why you find the context method unintuitive? It's designed exactly for this. You return the data you want from a loder as context, and then its available in any descendant route.. it's pretty ergonomical alternatively, you could use tanstack/query, calling fetchQuery/ensureQueryData on the parent and accessing it from the children with useQuery or directly via queryClient in the loaders
other-emerald
other-emeraldOP4mo ago
Ya, i've been playing around with it since asking and think I was making it more complicated than it needed to be. I didn't realize before that data returned from beforeLoad gets appended to the context, and I was trying to manipulate the context by creating setter methods on it for the data I needed. Now that I just return the data its much simpler and makes sense.

Did you find this page helpful?