[SOLVED] How to use Lazy query with React-query? – Stack Overflow


This Content is from Stack Overflow. Question asked by webber

I am using React-query for my API calls. I am wondering if there is a way to call the query in a lazy way.

Meaning to call the query only when a query param changes.

This is what I currently have; I am using a hack with the “useEffect” where if recipeName changes, then run the refetch function.

export const searchRecipeByName = async (recipeName: string) => {
  return await api.get(
  const [recipeName, setRecipeName] = useState("");

  const { data, refetch } = useQuery(
    () => searchRecipeByName(recipeName),
    { enabled: false }

  useEffect(() => {
    if (!!recipeName) {
  }, [recipeName]);

  const handleOnSearchSubmit = async (recipeSearch: RecipeSearch) => {

Preferably, I would like to call the query in the “handleOnSearchSubmit” function.

I could create a custom useLazyQuery hook to handle this, but I’m wondering if React-query has a native way to handle this.


Preferably, I would like to call the query in the "handleOnSearchSubmit" function.

This is a quite imperative way of thinking about things, but react-query is more declarative. You don’t specify: "when I click that button, I want to fetch", but you just say: "these are the inputs I require" and react-query will refetch whenever those inputs change.

So what you want to do is put the things that you need for your query to run into the query key, so that react-query

  • caches it separately for every dependency
  • you can disable it as long as dependencies are not ready
const [recipeName, setRecipeName] = React.useState('')
const { isLoading, data } = useQuery(
  ['homePageSearchQuery', recipeName],
  () => searchRecipeByName(recipeName),
    enabled: !!recipeName

then, all you need to do is call setRecipeName in handleOnSearchSubmit, and react-query will do the querying.

some more things that you might want to consider:

  • if you want to avoid a hard loading state in between recipeName changes, set keepPreviousData: true
  • the above code will run a query every time the recipeName changes. If that doesn’t happen on a button click, but say whenever the user types something in an input field, you you want to debounce that. For those cases, consider something like useDebounce:
const [recipeName, setRecipeName] = React.useState('')
const debouncedRecipeName = useDebounce(recipeName, 1000)
const { isLoading, data } = useQuery(
  ['homePageSearchQuery', debouncedRecipeName],
  () => searchRecipeByName(debouncedRecipeName),
    enabled: !!debouncedRecipeName

This will let you input data and show it in a text field, but only creates a new cache entry / fires a new request after 1 second of inactivity.

  • if you want to have a button that submits the form, it’s best to "lift state up": Have a more global state that the query depends on, and some local state for the form. Once the user hits "submit", you save the user selection in the state that lives higher up, which will trigger a query. I like to use the url for this, and I have a complete codesandbox example here. The actual form state is uncontrolled in that example, but you can also use state or a form lib for that

This Question was asked in StackOverflow by webber and Answered by TkDodo, It is licensed under the terms of
CC BY-SA 2.5.CC BY-SA 3.0.CC BY-SA 4.0.

people found this article helpful. What about you?