You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
New useRoute hook for accessing data from specific routes
For example, let's say you have an admin route somewhere in your app and you want any child routes of admin to all have access to the loaderData and actionData from admin.
This usage is equivalent to calling useLoaderData and useActionData, but consolidates all route data access into one hook: useRoute.
Note: when calling useRoute() (without a route ID), TS has no way to know which route is the current route.
As a result, loaderData and actionData are typed as unknown.
If you want more type-safety, you can either narrow the type yourself with something like zod or you can refactor your app to pass down typed props to your AdminWidget:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Supersedes: #13896 , #10761
Relates to: #9854 , #13961
PR: #14407
New
useRoute
hook for accessing data from specific routesFor example, let's say you have an
admin
route somewhere in your app and you want any child routes ofadmin
to all have access to theloaderData
andactionData
fromadmin.
You might even want to create a reusable widget that all of the routes nested under
admin
could use:In framework mode,
useRoute
knows all your app's routes and gives you TS errors when invalid route IDs are passed in:useRoute
returnsundefined
if the route is not part of the current page:Note: the
root
route is the exception since it is guaranteed to be part of the current page.As a result,
useRoute
never returnsundefined
forroot
.loaderData
andactionData
are marked as optional since they could be accessed before theaction
is triggered or after theloader
threw an error:If instead of a specific route, you wanted access to the current route's
loaderData
andactionData
, you can calluseRoute
without arguments:This usage is equivalent to calling
useLoaderData
anduseActionData
, but consolidates all route data access into one hook:useRoute
.Note: when calling
useRoute()
(without a route ID), TS has no way to know which route is the current route.As a result,
loaderData
andactionData
are typed asunknown
.If you want more type-safety, you can either narrow the type yourself with something like
zod
or you can refactor your app to pass down typed props to yourAdminWidget
:Future work
handle
to result returned byuseRoute
(Can we make the `handle` property of RouteObject an interface? #13961)Beta Was this translation helpful? Give feedback.
All reactions