React Query's staleTime and gcTime Explained: Two Settings, Two Jobs
React Query has two commonly confused cache settings — staleTime and gcTime — that control entirely different aspects of data management. staleTime determines how long fetched data is considered fresh, preventing automatic background refetches during that window; it defaults to zero, meaning data is stale the moment it arrives. gcTime, formerly called cacheTime before v5, controls how long unused data lingers in memory after a component unsubscribes, defaulting to five minutes. Misusing these settings causes real bugs: setting staleTime to Infinity stops React Query from ever refetching on its own, while a long gcTime has no effect on how frequently active queries refresh. Developers are advised to treat the two as independent levers — one governing refetch frequency for active subscriptions, the other managing memory cleanup after components unmount.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.



Discussion (0)
Log in to join the discussion and vote.
Log in