Loading data in ReactRouter v7 - useLoaderData vs loaderData prop #12730
Replies: 2 comments 4 replies
-
AFAIK, the prop is preferred, but the hook is available so you can easily access the data deep in the component tree. Particularly, I think it's better to explicitly pass what you need to child components so they aren't coupled to the route. |
Beta Was this translation helpful? Give feedback.
-
Just to add up to this one. Having the following route: app/routes.tsx
app/routes/route-name/route-name-index.tsx
Then if I need the loader data in AnotherComponent, I would use userLoaderData and for the types I've done it in to ways: Option 1: Importing the loader type from the original route file:
Option 2: Importing the same Route type used in the home file:
I've started to move to option 2, as sometimes I get weird Vite issue where server code is getting added to the client code. Hope someone can give more information on what is the "correct" way to do it. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, since upgrading to ReactRouter v7, I'm struggling to understand what's the preferred API to load data.
Previously, we've used
useLoaderData<typeof loader>()
hook. Now I see in some places in the documentation that the default route component accepts alsoloaderData
property.loaderData
as a prop (thecreateRoutesStub
does not accept component that definesloaderData
)?Thank you
Beta Was this translation helpful? Give feedback.
All reactions