#8: How to Stop Unwanted API Calls with Stale Time in React Query

Thapa Technical
63 followers
3 Video Views·Oct 2, 2024
Hello what's up guys, today we are going to see
stale time in our 10 stack query version 5
Yes guys, today's video is very important
because in yesterday's video
we saw what is GC time
which is your cache and it is very important
but there was a problem
let me tell you
the problem was
fetch request
I request on this
I am getting the data from my cache
but still we are requesting
see I am removing this
I went to home and came here
see I am getting the data
I went to home and came here
we have a very good idea
that the data I am getting is not from my server
I am getting the data from my cache
because the advantage of react query
is that by default
the data you get from response
is stored on our local cache
and we are
very sure
we know that my
API data
is updated 3-4 times in a day
so every time
I come to this page
I don't have to request a call
to the server
I want to give some time
let's say after 30 minutes
let's say once
every 1 hour
I have to automatically request
I want that
I don't want
every time I come here
I don't want this request
because when I am getting the data from cache
why should I request to the server
so to tackle this problem
we take the help of stale time
but what is stale time? let's understand
so let's check it out
what is this stale time
first of all tell me what is stale
have you ever heard this word stale?
obviously you must have heard
when you cook food
mom cooks food in the afternoon
and when you eat at night
they say mom cook fresh
it is stale
or for some people
we eat the leftover rice
in the morning
I mean we fry the rice
so this is stale rice
so stale is a kind of stale thing
so there is something here
let's understand what is here
in react query
stale time is a configuration option
it is an option that determines
how long fetched
data is considered fresh
before it needs to be refetched
have you understood what is happening here
when you
hit the request to the API
request to the API
to show your data
in our case we are showing the data of Jason Clare
so when you hit the request
and when you got the response
so after that
the data you got
how long
fresh
this data
is up to date
how long you have to make it
so this is stale time
configuration
there is an option
whatever data you are fetching
for how long
data is fresh
means it is up to date
and before it needs to be
refetched, a simple example of this is
corona or when covid happened
what was happening in covid
let's imagine
after every one second
list was updated
earlier it was 100, now it is 101
so it means
the data which I fetched
a while ago, after just one second
what happened
it became 82, because within a one second
a new patient's data was added
so I have to show it on my system
are you understanding
so this is stale time
now see
how it works
when you fetch the data for the first time
and update it, it is considered fresh
obviously, the moment you hit the request
the data you got
it is fresh
means it was fresh
then stale data
means after the stale time duration
see this is important
there is an option
duration of stale time
if it ends
then that data is considered stale
and what is the default value
any data you are fetching
when it will be considered stale
now this data is not up to date
it is not up to date
although I don't want it to be
but now it is not
so the default time is 0
means the moment you fetched the data
within a 0
no, the data is stale
I mean the data is stale
and this is the beauty
the beauty of your react query
that it
constantly fetches
and tells you to keep fetching
it keeps running in the background
ok
I hope you got to know
yesterday was garbage collection time
today was stale time
if you don't know about garbage collection
then I highly recommend you to check out
yesterday's video
now I will take you to the example
I have already given you the example
and that is
when I know that
I will get the data from the first time
and after that
my data is stored on cache for 5 minutes
and whenever I go to fetch request
basically I request the react query
I am getting the data from cache
where am I getting the data
I am getting the data from here
then who is this request going to
why are you requesting
when you are giving me the data from cache
then why are you requesting the server
because there is no update in the server
do you understand
this request is going to
because in react query
the default time is
zero
the moment you request
it expires
now it is a stale data
and if the data is stale
then it tries to fetch the data from the server
so that
it can show the up-to-date data to the client
so that it can show the up-to-date data to the client
which is a good thing
which I will tell you in tomorrow's video
but today I have to tackle this problem
because I am very much sure
that I have to fetch it in one hour
I will show you
this is our dev tools
we have this stale option
I have only one query
that is post
it is stale now
it will be fresh for some time
wait a second
I will remove this
I will go to home
I will come here
it is so fast
that it is not giving a chance to come here
that it is so fast
it is always stale
I have to tackle this
we have an option
we have a stale time
let's say 5000
means 5 seconds
this is 5000 milliseconds
which is equivalent to 5 seconds
I will come here
data is fresh
stale value is 0
within 5 seconds
your data went from fresh to stale
let me refresh it
I can see 1 next to fresh
I can see 0 next to stale
means my data is fresh
when data is fresh
it will not request to server
let me increase it
let's go with
10 seconds
data is fresh
let me remove it
I will go here
I am removing it
I am going to inspect
let's go to network tab
it came on fetch
it went to home
it came on fetch
the moment it is fetched
it is fresh again
it went to home
it came on fetch
it went to home
let me keep it aside
it is fresh
it is stale again
there is no data
it is neither stale nor fresh
let me go to it
it went to request
it is fresh
I will go to fetch request
what happened here
are you noticing
after 10 seconds
I requested it
before 10 seconds
I went to fetch request
server did not request
one more thing
you notice
the moment I go to home
and click on fetch request
the data I got
is fresh again
now
whatever I do
nothing will happen
my request is not going
because we told react query
our data is not stale
data is not outdated
data is correct
after 10 seconds
you will get a new update
some new data will be added
then we will show it
this is your stale time
your data is fresh
your stale time
configuration is telling
till your data is fresh
we will not request
basically server will not get request
the data in cache
its good
why to request again and again
and one more thing
the moment you request
new data will get 10 seconds
whatever I do
nothing happens
and when you request
after 10 seconds
you can request again
this is funda
I hope you liked the video
and if you think
I will take 1 hour
convert 1 hour to millisecond
whatever it is
and keep doing
I don't know how much I did
but my data will be fresh
how much ever I do
my data will be fresh
this is beauty of stale time
till when
you have to keep your data fresh
so that we don't request server
and show cache data
this is very good
you never feel
data is fresh
always data is on the spot
loading effect
this is it
I hope
GC time and stale time
this is very important concept
you understood the difference
but still
if you have any doubt
let me know in the comment section
I will try to guide you
tomorrow's video
will be more important
because in tomorrow's video
I will tell you
the reason behind
data update
in react query
you will enjoy
I want to tell you
please check out react
10 stack query is very important
obviously react is important
if you like the video
press like
if you don't like
press dislike
let me know in the comment section
our next target is
7 lakh subscriber
we are very close to 7 lakh subscriber
please subscribe
and share
see you in tomorrow's video
till then guys thank you so much
take care
