Logo

dev-resources.site

for different kinds of informations.

TLDR; gcTime & staleTime in react-query

Published at
5/22/2024
Categories
react
reactnative
reactquery
javascript
Author
thechaudhrysab
Author
14 person written this
thechaudhrysab
open
TLDR; gcTime & staleTime in react-query

Here is a simple understanding of two very important concepts in react-query caching.

Prerequisites: Will be good if you have a very basic idea of what react-query is..

In React Query v5, the cacheTime option in React Query has been renamed to gcTime.

The gcTime and staleTime options in React Query serve different purposes in managing and controlling the cached data lifecycle.

TLDR; In summary, gcTime manages how long data can remain in the cache after it becomes unused before being garbage collected to free up memory, while staleTime determines how long data can be considered fresh before triggering a refetch to ensure data currency and accuracy. Both options play crucial roles in optimizing data caching, memory management, and data freshness in React Query applications.

gcTime:

gcTime (Garbage Collect Time) is used to determine how long data will remain in the cache after a query becomes unused.

By setting gcTime to a specific duration (in milliseconds), you control how long data stays in the cache after it is no longer actively used. Once the gcTime duration has passed, the data is considered eligible for garbage collection, effectively managing the cache size and memory consumption.

Once the specified gcTime duration elapses, the data is marked for garbage collection to prevent the cache from growing indefinitely. gcTime focuses on managing memory and ensuring that outdated or unused data is eventually removed from the cache to maintain optimal performance and memory usage.

staleTime:

staleTime is used to specify the duration for which data is considered fresh before it becomes stale and triggers a refetch. When a query result is accessed, React Query checks if the data is stale based on the staleTime value.

If the data is stale (i.e., the specified time has passed since the data was fetched), React Query initiates a background refetch to update the data (isFetching can be used to detect when the background refresh). staleTime is primarily used to control how often React Query should automatically refetch data to ensure it remains up to date. By default, if no staleTime is provided (or set to 0), the query will immediately refetch when it mounts.

If you provide a staleTime value (in milliseconds), the data will be considered fresh for that duration before a refetch is triggered. If you also want to handle scenarios where the initialData might not be totally fresh, you can use the initialDataUpdatedAt option. This option allows you to pass a JS timestamp indicating when the initialData was last updated. This allows React Query to make a decision on whether the data needs to be refetched based on both the staleTime and the timestamp of when the initialData was last updated.

reactquery Article's
30 articles in total
Favicon
Create an SSR Application with Vite, React, React Query and React Router
Favicon
TanStack query or RTK query.
Favicon
Simplified State Management with useRQState: A Better Alternative to useState
Favicon
Master React API Management with TanStack React Query: Best Practices & Examples
Favicon
How to Fetch Data Using Axios and React Query in ReactJS
Favicon
Efficient Refresh Token Implementation with React Query and Axios
Favicon
react-query swrjs alova In-Depth Comparison
Favicon
Mastering React Query. Simplifying Data Management in React with Separation Patterns
Favicon
Mastering React Query. Structuring Your Code for Scalability and Reusability
Favicon
Why you should try React Query?
Favicon
How to used Suspense?
Favicon
Infinite list loading πŸ€”, with React Query - useInfiniteQuery hook !
Favicon
Building a CRUD app with React Query, TypeScript, and Axios
Favicon
TLDR; Suspense in react-query
Favicon
TLDR; gcTime & staleTime in react-query
Favicon
Building Infinite Scrolling in React Js with React Query v5
Favicon
Optimized Infinite Scroll with Next.js 14 Server Actions and React Query
Favicon
Building a Todo List with TypeScript and React Query: A Comprehensive Guide
Favicon
React Safe Query - A lightweight, type-safe wrapper built around React Query
Favicon
React Query Mutations Offline React-Native
Favicon
Improving UX by using cache
Favicon
How to organize your API layer in React using React Query
Favicon
Conociendo a React Query
Favicon
Build a CRUD App in React with Tanstack Query and Material UI [Final]
Favicon
Build a CRUD App in React with Tanstack Query and Material UI [Part 2]
Favicon
Integrating React Query with Next.js πŸš€
Favicon
Build a CRUD App in React with Tanstack Query and Material UI [Part 1]
Favicon
React query for data streaming
Favicon
Manual Fetch with Tanstack Query
Favicon
Setting up React Query in your ReactΒ project

Featured ones: