最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to stop refetchInterval react query (useQuries) after 3 times repeat? - Stack Overflow

programmeradmin0浏览0评论

Here i am using react Query to fetch data. i have used in useQuries refetchInterval for 3000 miliseconds, so every 3 seconds it's refetch data. so when i display data it's changing automatically as we refetch data. but in my case i don't want to refetch data more then 3 times, suppose, i want to use {refetchInterval : 3000} only for 3 times then it will stop refetching data.

is there any way to do this. how can i fix it for only 3 times use refetchInterval options. ecah time it will refetch after 3 seconds and onec it refetch 3 times it will automatically stop and no more reftech will work.

Please Help me to fix this and break the refetchInterval after 3 times. ThankYou for your trying in advance!

  const fetchData = () => {
            const rA=  devices?.map(async (id) => {
                const info = await axios.get(`/${id}`, myHeaders).then((res) => {
                    return res.data;
                })
                return info
                
            })
            
            return rA;
        };
    
        const { data } = useQuery("post", fetchData,
            { refetchInterval: 3000 });

Here i am using react Query to fetch data. i have used in useQuries refetchInterval for 3000 miliseconds, so every 3 seconds it's refetch data. so when i display data it's changing automatically as we refetch data. but in my case i don't want to refetch data more then 3 times, suppose, i want to use {refetchInterval : 3000} only for 3 times then it will stop refetching data.

is there any way to do this. how can i fix it for only 3 times use refetchInterval options. ecah time it will refetch after 3 seconds and onec it refetch 3 times it will automatically stop and no more reftech will work.

Please Help me to fix this and break the refetchInterval after 3 times. ThankYou for your trying in advance!

  const fetchData = () => {
            const rA=  devices?.map(async (id) => {
                const info = await axios.get(`https://www.roads.com/road/api/roadControl/${id}`, myHeaders).then((res) => {
                    return res.data;
                })
                return info
                
            })
            
            return rA;
        };
    
        const { data } = useQuery("post", fetchData,
            { refetchInterval: 3000 });
Share Improve this question asked Oct 17, 2022 at 8:55 Sabbir AhhmedSabbir Ahhmed 991 gold badge1 silver badge10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 15

This should work

const countRef = useRef(0)
const { data } = useQuery("post", () => {
  countRef.current += 1
  return fetchData()
}, { refetchInterval: () => countRef.current < 3 ? 3000 : false });
发布评论

评论列表(0)

  1. 暂无评论