ESLint warns React Query mutation must be included in dependency array

Viewed 125

I am using react-query library and I get ESLint useEffect dependency warning. my code is something like this:

const postProductMutation = useMutation(...);

useEffect(() => {
    postProductMutation.mutateAsync()
}, [])

now, as ESLint says, I should put postProductMutation in dependency array. but if I do, I will create an infinite loop. any solution?

1 Answers

you should destruct the value returned from useMutation and include that in dependency array.

  const { mutateAsync } = useMutation(...);

  useEffect(() => {
    mutateAsync();
  }, [mutateAsync]);
Related