T
TanStack10mo ago
extended-salmon

Change parent layout based on child route matching

Hi, I have a parent layout component and I need to render it a bit differently if a certain child route is active. I've tried to do useLoaderData({ from: "child route" }) but it throws if the child route is not matched. Is it recommended to un-nest from the parent routes and just render the layout in the child route? If I do that, I can't grab the parent loaded data which means I have to repeat the parent data loading in the child route, it's not a problem, but I just wanted to know if there's a better way. Cheers
3 Replies
conscious-sapphire
conscious-sapphire10mo ago
Having the same issue trying to populate a breadcrumb in the layout based on child data, have tried useLoaderData, importing the Route but none seems to be the right approach
wise-white
wise-white10mo ago
I think you should be able to use useMatches for the breadcrumbs
conscious-sapphire
conscious-sapphire10mo ago
useChildMatches hook | TanStack Router React Docs
The useChildMatches hook returns all of the child objects from the closest match down to the leaf-most match. It does not include the current match, which can be obtained using the useMatch hook. [!IM...

Did you find this page helpful?