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

ESLint warns React Query mutation must be included in dependency array

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?

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

>Solution :

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

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

  useEffect(() => {
    mutateAsync();
  }, [mutateAsync]);
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