Automatically refetching with React Query

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

A super cool feature of React Query is that we can auto refetch on a specified interval.
This could be useful if you have quickly changing data that needs to be rechecked every minute.
In our example, we'll call a random API endpoint, meaning every request has new data, and showcase whatever is in that refetch.
It will look like this:

To use the auto refetch mode, you can pass an optional parameter to the React Query hook called refetchInterval. The value is in milliseconds.
const {isLoading, data} = useQuery(
'vehicle',
async () => {
const {data} = await axios.get(
'https://random-data-api.com/api/vehicle/random_vehicle'
);
return data;
},
{
refetchInterval: 6000,
}
);
In the above example, we will query the random data API and ask for a random vehicle. This call will refetch the data every 6000 milliseconds.
When implementing code like this, be aware that this can be heavy on your API's and one should be very wise about when to use this approach.
React Query comes with more of these refetch functions that we can leverage.
By default, it will auto refetch every time the window focusses, for instance, let's take a look at what other options there are:
refetchInterval: See above examplerefetchIntervalInBackground: When set to true, the above function will even call when the tab is in the backgroundrefetchOnMount: You can set this to false to don't do the initial mount loadingrefetchOnWindowFocus: Will refetch every time the window focus is set. You can set this to falserefetchOnReconnect: Will refetch once a connection has been remadeBetween all of these, you can mix when data should be loaded.
You can try the refetch out in this Sandbox.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter