📦 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.
This commit is contained in:
14
shared/hooks/useRunOnce.ts
Normal file
14
shared/hooks/useRunOnce.ts
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
import { useEffect, useRef } from "react";
|
||||||
|
|
||||||
|
const useRunOnce = (callback: () => void | Promise<void>) => {
|
||||||
|
const calledRef = useRef(false);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (calledRef.current) return;
|
||||||
|
calledRef.current = true;
|
||||||
|
|
||||||
|
void callback();
|
||||||
|
}, [callback]);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default useRunOnce;
|
||||||
Reference in New Issue
Block a user