Capítulo 38 de 80
A worked walk-through of the default cache lifecycle (staleTime: 0, gcTime: 5min) for a single ['todos'] query, illustrating exactly when a mount is a hard load vs. an instant cache hit, and when a query is finally evicted.
['todos'] entry → hard loading state, real network request, result cached and immediately marked stale (staleTime: 0).status/isFetching because they're both subscribed to the same underlying query, so both update together regardless of whether the two fetchTodos calls are literally the same function reference.gcTime countdown (default 5 minutes) starts toward eviction.gcTime elapses: cached data renders instantly again (no hard loading state) while a background fetch refreshes it — the GC countdown is cancelled since there's an active observer again.gcTime: the cache entry for ['todos'] is deleted; the next mount after that point starts the whole cycle over from a genuine hard loading state.| Event | Loading state? | Network request? | GC timer |
|---|---|---|---|
| 1st mount, empty cache | Hard loading | Yes | — |
| 2nd concurrent mount, same key | None (cache hit) | Yes (background) | — |
| All observers unmount | — | — | Starts (gcTime) |
New mount before gcTime expires | None (cache hit) | Yes (background) | Cancelled |
No mount within gcTime | — | — | Entry deleted |
gcTime, no matter how many prior instances unmounted, is a cache hit with a background refresh.gcTime's countdown resets to "not scheduled" the instant a new observer mounts — it's not a rolling deadline from the query's creation, it's purely about "how long can this sit with zero observers."staleTime/gcTime defaults this walkthrough is built on.status/isFetching fields referenced throughout.