From 41babd10a1fede71b0d6a86117a4f94b69d50e7a Mon Sep 17 00:00:00 2001 From: Rafi Arrafif Date: Sun, 31 Aug 2025 23:33:04 +0700 Subject: [PATCH] :package: add skip react srictmode Adding new hooks as a replacement for `useEffects(() => {}, [])` in React by adding React strict mode safety, which means that this function will only be executed once, even in development mode. --- shared/hooks/useRunOnce.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 shared/hooks/useRunOnce.ts diff --git a/shared/hooks/useRunOnce.ts b/shared/hooks/useRunOnce.ts new file mode 100644 index 0000000..ba548dc --- /dev/null +++ b/shared/hooks/useRunOnce.ts @@ -0,0 +1,14 @@ +import { useEffect, useRef } from "react"; + +const useRunOnce = (callback: () => void | Promise) => { + const calledRef = useRef(false); + + useEffect(() => { + if (calledRef.current) return; + calledRef.current = true; + + void callback(); + }, [callback]); +}; + +export default useRunOnce;