TanStackT
TanStack15mo ago
10 replies
conventional-black

using return value of useQuery as a queryKey

export const useGetCurrentUser = () => {
  const { data } = useQuery({
    queryFn: () => query(),
    queryKey: ['current_user', 'user', data.id],
  });

  return { user: data as UserResponse };
};


This obviously does not work since data is used before its declaration.

Is there a way to use data.id as a queryKey? I fetch the user by cookie information, so don't need to use an
id
, but want to use its
id
as a queryKey, so that if I fetch a user with useGetUserById function, which accepts an
id
, then I would hit the cache instead of making a request for the data I already have.
Was this page helpful?