Dependent Queries in React

From the TanStack Query cheat sheet · Queries · verified Jul 2026

Dependent Queries

Queries that depend on other queries

typescript
// Sequential dependent queries
function UserProjects({ userId }) {
  // First query
  const userQuery = useQuery({
    queryKey: ['user', userId],
    queryFn: () => fetchUser(userId)
  })

  // Second query depends on first
  const projectsQuery = useQuery({
    queryKey: ['projects', userQuery.data?.id],
    queryFn: () => fetchProjects(userQuery.data.id),
    enabled: !!userQuery.data?.id // Only run when user data is available
  })

  return (
    <div>
      <h1>{userQuery.data?.name}</h1>
      <ul>
        {projectsQuery.data?.map(project => (
          <li key={project.id}>{project.name}</li>
        ))}
      </ul>
    </div>
  )
}
💡 Use enabled option to control query execution order
⚡ Each query maintains its own cache independently
📌 Handle loading states for each dependent query
🟢 Structure query keys hierarchically for clarity
queriesdependentsequential

More React tasks

Back to the full TanStack Query cheat sheet