UNPKG

@primer/react

Version:

An implementation of GitHub's Primer Design System using React

40 lines (39 loc) 963 B
import { c } from "react-compiler-runtime"; import { useEffect, useRef } from "react"; //#region src/internal/hooks/useEffectCallback.ts /** * Create a callback that can be used within an effect without re-running the * effect when the values used change. The callback passed to this hook will * always see the latest snapshot of values that it uses and does not need to * use a dependency array. */ function useEffectCallback(callback) { const $ = c(4); const savedCallback = useRef(callback); let t0; let t1; if ($[0] !== callback) { t0 = () => { savedCallback.current = callback; }; t1 = [callback]; $[0] = callback; $[1] = t0; $[2] = t1; } else { t0 = $[1]; t1 = $[2]; } useEffect(t0, t1); let t2; if ($[3] === Symbol.for("react.memo_cache_sentinel")) { t2 = (...t3) => { const args = t3; return savedCallback.current(...args); }; $[3] = t2; } else t2 = $[3]; return t2; } //#endregion export { useEffectCallback };