UNPKG

@primer/react

Version:

An implementation of GitHub's Primer Design System using React

51 lines (48 loc) 1.25 kB
import { c } from 'react-compiler-runtime'; import { useState, useRef, useEffect } from 'react'; /** * 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; } export { useRenderForcingRef };