Skip to content

Instantly share code, notes, and snippets.

@zaverden
Created November 25, 2022 17:02
Show Gist options
  • Save zaverden/120b768cf6bf6e39243c9ed560a1e8c8 to your computer and use it in GitHub Desktop.
Save zaverden/120b768cf6bf6e39243c9ed560a1e8c8 to your computer and use it in GitHub Desktop.
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