Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: useLoaderData doesn't ascend route tree when used on a route element[|component?] #10668

Closed
eckdanny opened this issue Jul 4, 2023 · 1 comment
Labels

Comments

@eckdanny
Copy link

eckdanny commented Jul 4, 2023

What version of React Router are you using?

6.14.1

Steps to Reproduce

StackBlitz

(I forked the Data Router example)

Expected Behavior

The verbiage "...nearest ancestor route loader" makes me think ascending the route tree for resolved loader data, -exactly how useLoaderData() behaves when used in a component. I thought this would apply to Routes too.

Use case I have in mind is not data-loading (#10616) or composing queries from resolved tributary data (#9188 (comment)). I'm thinking about tabbed interfaces (player/:id/<offense>|<defense>) or wizards (thing/new/<1-of-2>|<2-of-2>) where the underlying data might exist at the :id or new segment respectively, but the leaf views are RRD Routes.

I kinda get what I want doing:

function useLoaderDataFromFirstAncestor() {
  const matches = useMatches();
  return matches.reverse().find((d) => Boolean(d.data))?.data;
}

but that doesn't feel right. Maybe I'm not interpreting docs correctly or this is usage question, but I started with a bug report.

Actual Behavior

When I use useLoaderData() on Route components/elements and don't define a loader, it ascends the tree.

@eckdanny eckdanny added the bug label Jul 4, 2023
@timdorr
Copy link
Member

timdorr commented Jul 4, 2023

That comment is referring to other components in the tree rendered by the Route's element. It does not cross the Route boundary upwards. The docs make this pretty clear. (Note: You should be reading the docs, not the source code comments. They're not always updated/correct and have fewer people checking them for accuracy.)

@timdorr timdorr closed this as completed Jul 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants