#Router with Tanstack Query - Why not useLoaderData ?

2 messages · Page 1 of 1 (latest)

ripe oxide
#

If I have a route that looks like this:

const userQueryOptions = (userId: string) => {
  queryKey: ["userDetails", { userId: userId }],
  queryFn: async () => await apiClient.get_user_details(userId),
});

const userDetailsRoute = createRoute({
  getParentRoute: () => rootRoute,
  path: "userDetails",
  loaderDeps: ({ search }) => ({
    userId: search.userId
  }),
  loader: async ({ context: { apiClient }, deps: { userId }}) => {
    await apiClient.ensureQueryData(userQueryOptions(userId));
  },
  component: UserDetailsComponent,
});

Then the recommendation I've seen to get the data in my component is to use

const { data } = useSuspenseQuery(userQueryOptions(userId);

Which means that I need to grab the userId out of the routes search params again to do so.

Is it possible to just use useLoaderData or something similar so that I don't need to parse the search params again in the component, and instead just use the data I already told the loader to fetch?

limpid raft
#

you must use useQuery or useSuspenseQuery to get all features react-query offers