Skip to content

Instantly share code, notes, and snippets.

@joenas
Created Apr 20, 2022
Embed
What would you like to do?
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