Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 38 ยท Queries and Keys

Dependent Queries and the enabled Option

Run one query after another with the enabled option: chain requests, wait for user input, understand isLoading versus isPending for idle queries, and avoid waterfalls.

Some data cannot be requested until you have other data. You fetch a user, and only then the projects that belong to that user. You wait for the user to type three characters before searching. These are dependent queries: a query that must stay idle until a condition becomes true. TanStack Query handles this with the enabled option.

The enabled option

enabled is a boolean. While it is false, the query does not fetch automatically, not on mount, not on window focus, not on reconnect. When it turns true, the query behaves like a normal query and fetches if it needs to.

JSX
const { data: user } = useQuery({
  queryKey: ["user", email],
  queryFn: () => fetchUserByEmail(email),
});

const userId = user?.id;

const { data: projects } = useQuery({
  queryKey: ["projects", userId],
  queryFn: () => fetchProjects(userId),
  enabled: !!userId,          // wait until we know the id
});

The second query is idle until userId exists. When the first one finishes, the component re-renders, enabled becomes true and the projects request starts.

What the status looks like while disabled

A disabled query with no cached data is isPending (it has no data) but not isFetching (nothing is in flight). If you show a spinner when isPending is true, a disabled query would spin forever. The flag that means "pending and fetching" is isLoading:

Situation isPending isFetching isLoading
Disabled, no data true false false
First request in flight true true true
Has data, background refetch false true false

Use isLoading for "show a loading indicator for the first request" and use fetchStatus === "idle" plus isPending to detect "waiting for input". The status lesson has the full table.

Chaining two queries

Here the second request depends on the result of the first. The log shows the order, and you can see the second request begin only after the first ends.

A user, then their projects

Notice the two requests happen one after the other. That is a request waterfall: total time is the sum of both. It is unavoidable here, because the second request needs the first answer. But you should check whether it is really necessary.

Avoiding unnecessary waterfalls

A waterfall is a design smell when the second request does not truly need the first response. A common fix is a backend endpoint that accepts what you already know:

  • If you already know the user id from the URL or a session, do not fetch the user just to read id. Request /projects?userId=42 right away.
  • If two queries only look dependent because of component nesting, move both calls up to the parent so they start in parallel (see Parallel Queries), or prefetch the child data in a router loader.
  • If a server can return the user with their projects in one call, one query is simpler than two.

Use enabled when the dependency is genuine: the second request really needs a value from the first, or from the user.

Waiting for user input

enabled is just as useful for search boxes, where you do not want a request for an empty string:

Search that waits for 3 characters

Each new term is a new key, so each search gets its own cache entry and going back to a previous term is instant. For real search boxes you would also debounce the input so you do not start a request on every keystroke; debouncing is a state concern, handled before the value reaches the key.

enabled does not stop observers from reading the cache

A disabled query still returns whatever data is already in the cache for its key. enabled: false only prevents fetching. This makes it a good tool for "read this if we have it, but do not request it": for example, showing a user's name from an existing ["user", id] entry without causing a request.

Two related behaviors to remember:

  • refetch() still works on a disabled query, so you can build "load on click" buttons: set enabled: false and call refetch() from the handler.
  • invalidateQueries does not refetch disabled queries automatically; they stay idle until enabled.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does enabled: false do?

    A disabled query does not fetch on mount, focus or reconnect, but it can still return cached data.
  2. 2. A disabled query has no cached data. Which flags are true?

    There is no data yet (isPending) but no request is running (isFetching false), so isLoading is also false.
  3. 3. How do you build a 'load on button click' query?

    refetch() runs a disabled query on demand.
  4. 4. When is chaining two queries a design smell?

    Needless dependencies create waterfalls that make the page slower.

Summary

  • enabled controls whether a query may fetch automatically; use it for genuine dependencies and for waiting on user input.
  • A disabled query without data is isPending but not isFetching or isLoading.
  • Chained queries form a waterfall, so remove dependencies you do not need.
  • Disabled queries still serve cached data, and refetch() can run them manually.
  • Combine enabled with search terms in the key, and debounce the input.

Next: Parallel Queries