Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Query Caching and Stale Data | Fetching and Caching Data
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
TanStack Query Server State Management in React

bookQuery Caching and Stale Data

When you fetch data using TanStack Query in React, the library does more than just retrieve and display information. It also caches the data locally in memory. This means that when a query is made, TanStack Query stores the result so that if the same query is requested again—such as when you revisit a component or tab—the cached data can be used instantly, without making another network request. This approach improves the responsiveness of your app and reduces unnecessary traffic to your backend.

The caching behavior is controlled by two important options: staleTime and gcTime. The staleTime option determines how long fetched data is considered fresh. During this period, TanStack Query will serve the cached data and avoid refetching it from the server. Once the staleTime expires, the data is marked as stale, and the next time you access it, TanStack Query may trigger a background refetch to update the data. The gcTime option, on the other hand, determines how long the cached data remains in memory after it is no longer used by any component. If no components are using the data and the gcTime elapses, TanStack Query removes the cached data from memory entirely. Adjusting these values helps you balance between data freshness and performance.

Imagine you have a React app with a user profile page and a dashboard. Both components fetch the same user data using TanStack Query. If you set a staleTime of 5 minutes, and you first visit the profile page, the data is fetched and cached. If you then navigate to the dashboard within 5 minutes, TanStack Query immediately provides the cached data, making the transition seamless and fast. No new network request is made until the staleTime expires. If you stay away from both components for longer than the gcTime (for example, 10 minutes), the cached data is removed, and the next visit to either component will trigger a new fetch.

question mark

Which of the following best describes what happens if you increase the staleTime value for a query in TanStack Query?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 2

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

bookQuery Caching and Stale Data

Deslize para mostrar o menu

When you fetch data using TanStack Query in React, the library does more than just retrieve and display information. It also caches the data locally in memory. This means that when a query is made, TanStack Query stores the result so that if the same query is requested again—such as when you revisit a component or tab—the cached data can be used instantly, without making another network request. This approach improves the responsiveness of your app and reduces unnecessary traffic to your backend.

The caching behavior is controlled by two important options: staleTime and gcTime. The staleTime option determines how long fetched data is considered fresh. During this period, TanStack Query will serve the cached data and avoid refetching it from the server. Once the staleTime expires, the data is marked as stale, and the next time you access it, TanStack Query may trigger a background refetch to update the data. The gcTime option, on the other hand, determines how long the cached data remains in memory after it is no longer used by any component. If no components are using the data and the gcTime elapses, TanStack Query removes the cached data from memory entirely. Adjusting these values helps you balance between data freshness and performance.

Imagine you have a React app with a user profile page and a dashboard. Both components fetch the same user data using TanStack Query. If you set a staleTime of 5 minutes, and you first visit the profile page, the data is fetched and cached. If you then navigate to the dashboard within 5 minutes, TanStack Query immediately provides the cached data, making the transition seamless and fast. No new network request is made until the staleTime expires. If you stay away from both components for longer than the gcTime (for example, 10 minutes), the cached data is removed, and the next visit to either component will trigger a new fetch.

question mark

Which of the following best describes what happens if you increase the staleTime value for a query in TanStack Query?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 2
some-alt