UNPKG

@primer/react

Version:

An implementation of GitHub's Primer Design System using React

47 lines (46 loc) 1.23 kB
import { c } from "react-compiler-runtime"; import { useEffect, useRef, useState } from "react"; //#region src/hooks/useRenderForcingRef.ts /** * There are certain situations where a ref might be set after the current render cycle for a * component has finished. e.g. a forward ref from a conditionally rendered child component. * In these situations, we need to force a re-render, which is done here by the useState hook. * @type TRef The type of the RefObject which should be created. */ function useRenderForcingRef(value) { const $ = c(5); const [refCurrent, setRefCurrent] = useState(value || null); const ref = useRef(null); let t0; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { t0 = (newRef) => { ref.current = newRef; setRefCurrent(newRef); }; $[0] = t0; } else t0 = $[0]; const setRef = t0; let t1; let t2; if ($[1] !== refCurrent) { t1 = () => { ref.current = refCurrent; }; t2 = [refCurrent]; $[1] = refCurrent; $[2] = t1; $[3] = t2; } else { t1 = $[2]; t2 = $[3]; } useEffect(t1, t2); let t3; if ($[4] === Symbol.for("react.memo_cache_sentinel")) { t3 = [ref, setRef]; $[4] = t3; } else t3 = $[4]; return t3; } //#endregion export { useRenderForcingRef };