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

How can I avoid "missing dependency" in useEffect by useRef?

  const increaseNumber = useRef({});
  increaseNumber.current = async () => {
    await setTimeout(() => {
      setNum(num + 1);
    });
  };
  useEffect(() => {
    increaseNumber.current();
  }, []);

Error at current():
(property) React.MutableRefObject<{}>.current: {}
This expression is not callable.
Type ‘{}’ has no call signatures.ts(2349)

>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

You are initializing increaseNumber to an empty object instead of a function.

const increaseNumber = useRef({});

Try:

  const increaseNumber = useRef(async () => {
    await setTimeout(() => {
      setNum(num + 1);
    });
  });
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