Created
November 25, 2022 17:02
-
-
Save zaverden/120b768cf6bf6e39243c9ed560a1e8c8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect, useRef } from "react"; | |
export function useCallOnce(fn: () => void) { | |
const isCalledRef = useRef(false); | |
useEffect(() => { | |
if (isCalledRef.current) return; | |
isCalledRef.current = true; | |
fn(); | |
}, []); // eslint-disable-line react-hooks/exhaustive-deps | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment