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

Time:07-19

  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)

CodePudding user response:

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);
    });
  });
  • Related