Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 38 ยท Projects and Practice

Project: Live Dashboard

Build a live dashboard with TanStack Query: refetchInterval polling, select to derive data, dependent queries, pausing when hidden and a manual refresh button.

Dashboards show data that changes while the user watches. This project builds a small server-monitoring dashboard that polls every two seconds, derives summary numbers with select, loads details only for the server you pick, and pauses polling when you ask it to. A fake server generates random but bounded numbers, so you can see every refetch happen.

Ideas you will practice

  • refetchInterval for polling, and refetchIntervalInBackground for tabs that are not focused.
  • select to compute derived values without storing them twice.
  • enabled and dependent queries for a detail panel.
  • isFetching versus isPending to show a quiet "updating" indicator instead of a spinner.
  • structuralSharing: unchanged parts of the data keep their identity, so only changed rows re-render.

The app

Live dashboard

Watch the "updating..." label blink every two seconds while the bars move. Uncheck Auto refresh and the polling stops; Refresh now still works because it invalidates the key. Click a server: its history loads once and is cached for ten seconds, so clicking back and forth does not refetch.

One query, two views

App and Summary use the same key ["servers"], so they share one cache entry and one request per interval. The select option runs on the cached data and returns derived values. Components using select re-render only when the selected result changes (compared structurally), so the summary cards stay still when only a bar changes in the table and the numbers are identical.

flowchart LR
  A["Timer every 2 s"] --> B["fetchServers"]
  B --> C["cache ['servers']"]
  C --> D["Table: whole list"]
  C --> E["Summary: select derives totals"]

Both views read one cache entry; select does the derivation, not another request.

Polling rules of thumb

  • Pick the interval from the data. CPU graphs may need 2 s; a billing total may need a minute. A function is allowed: refetchInterval: (query) => query.state.data?.done ? false : 1000 polls until a job finishes, then stops.
  • Background tabs. By default polling pauses when the tab is hidden. Set refetchIntervalInBackground: true only if you truly need updates while hidden, for example to update the tab title.
  • Do not overlap. TanStack Query waits for the running fetch; it does not start a second one for the same key, so slow servers do not pile up requests.
  • Prefer push for real-time. For chat or trading, use WebSockets and write incoming messages with setQueryData. Polling is a good default for "fresh enough" data.
  • Mind the cost. Every open tab polls. Combine with staleTime and an interval you can afford at your traffic.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the refetchInterval option do?

    refetchInterval polls the query function at the given number of milliseconds while a component is using the query.
  2. 2. Two components use the same key but one adds select. How many requests run per interval?

    Observers with the same key share a query; select only transforms the cached data for that observer.
  3. 3. How can polling stop automatically when a job is done?

    refetchInterval accepts a function that returns a number or false, so you can stop polling once the data shows completion.
  4. 4. Why use isFetching instead of isPending for the 'updating...' label?

    Background refetches keep the old data, so isPending is false; isFetching tells you a request is running.

Summary

  • refetchInterval turns a query into a poller; a function lets it stop itself.
  • Same key means one shared request, and select derives views from it.
  • Use isFetching for quiet indicators and isPending for the first load.
  • Detail queries use enabled and a staleTime to avoid repeated loading.
  • Polling pauses in hidden tabs unless you opt in.

Next: Practice: Cache Challenges