Redux & Redux Toolkit Course
Redux & Redux Toolkit
/
Intermediate

Polling & Refetching

Definition

Features in RTK Query that allow data to be automatically refetched from the server based on time intervals, window focus events, or network reconnection.

Explain Like I'm New

Some data changes constantly, like a live sports score or a stock ticker. RTK Query can be told: 'Automatically fetch this endpoint every 3 seconds' (Polling), or 'Refetch this data every time the user clicks back to this browser tab' (Refetch on Focus).

Real World Example

A cryptocurrency dashboard. You set `pollingInterval: 5000` on the Bitcoin price query. Without writing a single `setInterval` or `useEffect`, the price updates automatically every 5 seconds.

Common Use Cases

  • •Live dashboards
  • •Keeping stale data fresh
  • •Mobile-style background fetching

Interactive Example

Loading...
Console output will appear here...

Interview Questions

basic

  • What option do you pass to a query hook to make it fetch every 10 seconds?

intermediate

  • Why is `refetchOnFocus` useful for web applications?

Flash Cards

Question

What option?

Click to reveal answer
Answer

`{ pollingInterval: 10000 }` (It is measured in milliseconds).

Question

Why refetchOnFocus?

Click to reveal answer
Answer

If a user leaves your app open in a tab, goes to lunch, and comes back, the data is likely stale. Refetching exactly when they click back to the tab ensures they immediately see fresh data without having to refresh the page manually.