Description

Sometimes an event should only be called once. Such as init functions. To do this we can use useEffectOnce which circumnavigates react strict mode by using a Closure ref to see if its already been called.
?

Reach for when

You have a function which should only run once.

Code

function useEffectOnce(effect) {
  const hasRun = useRef(false);
  useEffect(() => {
    if (hasRun.current) return;
    hasRun.current = true;
    return effect();
  }, []);
}