score:33

Accepted answer

The function of useEffect (EffectCallback type) should return void or () => void | undefined.

function useEffect(effect: EffectCallback, deps?: DependencyList): void;

type EffectCallback = () => (void | (() => void | undefined));

In your case, you returning void => boolean:

// void => boolean
return () => (isMounted.current = false);

To fix it, add scope to the statement of the cleaning function:

const useIsMounted = () => {
  const isMounted = React.useRef(false);
  React.useEffect(() => {
    isMounted.current = true;
    return () => {
      isMounted.current = false;
    };
  }, []);
  return isMounted;
};

Related Query

More Query from same tag