Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Query Caching and Stale Data | Fetching and Caching Data
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

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 2

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Suggested prompts:

Can you explain the difference between `staleTime` and `gcTime` in more detail?

How do I choose appropriate values for `staleTime` and `gcTime` in my app?

What happens if I set `staleTime` and `gcTime` to very high or very low values?

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

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 2
some-alt