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: to fix this, wrap the initialization of constant in its own useMemo()

I get this eslint warning:

ESLint: The ‘variables’ object makes the dependencies of useEffect
Hook (at line 36) change on every render. To fix this, wrap the
initialization of ‘variables’ in its own useMemo()
Hook.(react-hooks/exhaustive-deps

for the following piece of code:

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

const MyComponent = () => {
  // ...
  const variables = {
    orgID: org.id,
    projectIDs: projects.map(p => p.id)
  };
  const { data, error, loading } = myGraphqlQuery({ variables });

  useEffect(
    () => myOtherFunction(client, channel, query, variables),
    [variables, channel, client],
  );
  // ...
}

I don’t get it, what should I do here? What’s the point of using useMemo for the const?

>Solution :

You should wrap the initialization of your variable with a useMemo.

const variables = useMemo(() => {
  return {
    orgID: org.id,
    projectIDs: projects.map(p => p.id)
  };
, [
// add the necessary dependencies here
]);
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