In queryOptions
you can set the staleTime
of a query to set how long it takes for data to be considered stale and warrant a refetch.
By default, React Query sets this value to 0
.
But, different queries will need different times set depending on how often the data on the server is being updated.
[00:00] Go back to the definition of the employee query and add the stale time property, which defines in milliseconds the time after the data is considered stale. So since this is in milliseconds, we'll multiply 1,000 by, let's say, 3 seconds. So for 3 seconds, the data that [00:19] is being loaded from the server is going to be considered fresh enough that there is no need for it to reload. So let's save the file, and we will see that this query is being green, is being fresh for 3 seconds. And after the time, it's moving back to the stale state. So let's take a look at how would [00:39] this behave if we gave some more time. So this is going to be 15 seconds. I'm also going to open the network tab just to see more information about this query over here. So we're interested in how many requests are actually going to be executed. So let's [01:00] move to the welcome page, and let's save the file. Let's refresh the application. So we can see that there is the first employee's request over here. And when we move to budgets and we move back to the employees, we can see that no additional requests being made for [01:20] employees. But as we're waiting for the 15 seconds to pass, we're waiting for this query to become stale. And now if we walk into the budget, we can see that when the query is stale, the first moment that a new observer, a new consumer of the query, the new component [01:39] is requiring it, then this is an opportunity for the query client to refresh the data in the background. 2 important things to note here. As you can imagine, you will want to configure different values for stale time for different queries depending on what data is [01:59] being fetched. Another important thing is that if you don't provide what is the value of the stale time, React query by default pushes 0 since most often when we load any data from the server, it could potentially, pessimistically, be already out of sync with the new data on the [02:19] server. By the time that we have loaded the data from the server it could have already changed. Sometimes you will want to put the infinity if you want a query never to become stale.
Member comments are a way for members to communicate, interact, and ask questions about a lesson.
The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io
Be on-Topic
Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.
Avoid meta-discussion
Code Problems?
Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context
Details and Context
Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!