Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

react-query customHook refetchOnWindowFocus

I am creating individual hooks using react-query. Where would I add in refetchOnWindowFocus: false ? as it is not currently being read and the data is being re-fetched when returning to the window.

    const useFetchOverview = () => {
        return useQuery(["userData", { refetchOnWindowFocus: false }],
            async () => {
                const { data } = await axios({
                    url: useFetchOverviewUrl,
                    headers: { ...getHeaders(reduxState) },
                    method: "get"
                });
                return data;
            });
    };
    const { isLoading, data: userOverviewData } = useFetchOverview();

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

this should be third parameter after fuinction:

return useQuery(["userData"],
            async () => {
                const { data } = await axios({
                    url: useFetchOverviewUrl,
                    headers: { ...getHeaders(reduxState) },
                    method: "get"
                });
                return data;
            }, { refetchOnWindowFocus: false });

ex: useQuery(queryKey, queryFn?, options)

check this for your reference: https://tanstack.com/query/v4/docs/reference/useQuery?from=reactQueryV3&original=https://react-query-v3.tanstack.com/reference/useQuery

OR you can write it for global:

const queryClient = new QueryClient({
   defaultOptions: {
     queries: {
       refetchOnWindowFocus: false,
     },
   },
 })
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading