admin管理员组文章数量:1186284
I'm using react-query v3.13
to fetch data from API.
What I want to do is to fetch API regularly from a certain point (for example, when clicking the start button), not from the time of calling useQuery.
I tried the following ways.
- set
enabled
property tofalse
to disable automatic querying but it also disables re-fetching.
I could not find a way to re-enable/set theenabled
property totrue
. And I had to usesetTimeout
by myself to re-fetch regularly. - keep
enabled
property astrue
but I could not find a way to disable the initial fetching.
Is there any proper way to do this?
I'm using react-query v3.13
to fetch data from API.
What I want to do is to fetch API regularly from a certain point (for example, when clicking the start button), not from the time of calling useQuery.
I tried the following ways.
- set
enabled
property tofalse
to disable automatic querying but it also disables re-fetching.
I could not find a way to re-enable/set theenabled
property totrue
. And I had to usesetTimeout
by myself to re-fetch regularly. - keep
enabled
property astrue
but I could not find a way to disable the initial fetching.
Is there any proper way to do this?
Share Improve this question asked Mar 23, 2021 at 3:50 Ever DevEver Dev 2,1423 gold badges18 silver badges39 bronze badges 1 |4 Answers
Reset to default 19if you hardcode the enabled
option to false
, you can still use the refetch
method you get back from useQuery
to imperatively trigger a fetch. But the query will still be disabled, so no background updates.
if you want a lazy that is disabled, but enables when you call a function, the best way would be with a simple abstraction over useQuery with a local state:
const useLazyQuery = (key, fn, options) => {
const [enabled, setEnabled] = useState(false)
return [() => setEnabled(true), useQuery(key, fn, { ...options, enabled })]
}
For those coming to this post for disabling fetch on mount, especially in a server actions context where you are fetching initial data on the server side, the fix for me was to set refetchOnMount: false
.
const { data, isLoading, refetch } = useQuery({
queryKey: [`todo`, id],
queryFn: () => actions.getTodo(id),
initialData: initialTodo,
refetchOnMount: false,
});
I found "don't fetch on load or mount" especially common for queries associated with pop-ups or dialogs. A common scenario is when you don't want to fetch data on component mount or initial load, but rather when a specific UI element (like a dialog) is opened.
The solution that worked for me is to bind a variable to the dialog state and use it to control when the query is enabled. This means:
- It fetches data only when the dialog or popup is opened.
- It continues to refetch when query keys change or data becomes invalid, but only while the dialog remains open.
- It prevents unnecessary API calls when the dialog is closed.
Implementation
import { useState } from 'react';
import { useQuery } from 'react-query';
function YourComponent() {
const [isOpen, setIsOpen] = useState(false);
const { data, isPending, isError, error, refetch } = useQuery({
queryKey: [promptId, inputValue],
queryFn: async () => {
const data = await // Your API call here
return data;
},
// Note that we pass isOpen to enabled instead of `true` or `false`
enabled: isOpen,
});
// Rest of your component logic
}
A breakdown of what it means when enabled
option in the useQuery
hook is set to isOpen
:
- The query will only run when
isOpen
istrue
(i.e., when the dialog is opened). - If the dialog is closed (
isOpen
isfalse
), the query won't run, saving unnecessary API calls. - When the dialog is open, the query will behave normally, refetching when query keys change or data becomes invalid.
import { useBoolean } from "@chakra-ui/react";
import { useQuery } from "@tanstack/react-query";
function useLazyQuery(queryKey, {queryFn, onError}) {
const [Enable, setEnable] = useBoolean(false);
const Get = useQuery(queryKey, {
queryFn,
onError: typeof onError === "function" && onError,
enabled: Enable, // Habilitar la consulta solo si el filtro no está vacío
});
return { ...Get, enable: setEnable.on, disable: setEnable.off };
}
export default useLazyQuery;
本文标签: javascriptHow to prevent reactquery from fetching initially but enables refetchingStack Overflow
版权声明:本文标题:javascript - How to prevent react-query from fetching initially but enables refetching? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738265137a2072004.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
forceFetchOnMount
property in the previous version, but it's replaced byrefetchOnMount
in version 3. – Ever Dev Commented Mar 23, 2021 at 3:52