import React from "react"; type WithOptionalCleanup = { cleanup?: () => void; }; /** * returns a ref with an initial value that is lazily evaluated once * */ export const useRefWithInitialValue = (initialValueGetter: () => T) => { const ref = React.useRef(null); if (ref.current === null) { ref.current = initialValueGetter(); } React.useEffect(() => { return () => { (ref.current as unknown as WithOptionalCleanup)?.cleanup?.(); }; }, []); return ref; };