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(
`/recipes/complexSearch?apiKey=${process.env.NEXT_PUBLIC_SPOONACULAR_API_KEY}&query=${recipeName}&addRecipeInformation=true&fillIngredients=true`
);
};
const [recipeName, setRecipeName] = useState("");
const { data, refetch } = useQuery(
"homePageSearchQuery",
() => searchRecipeByName(recipeName),
{ enabled: false }
);
// HACK
useEffect(() => {
if (!!recipeName) {
refetch();
}
}, [recipeName]);
const handleOnSearchSubmit = async (recipeSearch: RecipeSearch) => {
setRecipeName(recipeSearch.search);
};
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.