Skip to content

Instantly share code, notes, and snippets.

@joenas
Created April 20, 2022 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joenas/7a03f64d84290efd59917140fd899fb7 to your computer and use it in GitHub Desktop.
Save joenas/7a03f64d84290efd59917140fd899fb7 to your computer and use it in GitHub Desktop.
React useOnce hook for one-time effects
import { useEffect, useRef } from 'react';
const useOnce = (effect) => {
const didUse = useRef(false);
useEffect(() => {
if (didUse.current === false) {
didUse.current = true;
effect();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
};
export default useOnce;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment