import {forwardRef, useRef, useImperativeHandle} from 'react'; import type {Ref, ReactElement} from 'react'; interface Props { useHook(): HookResult; } export interface ImperativeApi { readonly current: HookResult; } export const HookRunner = forwardRef( ({useHook}: Props, ref) => { const hookResult = useHook(); const valueRef = useRef(hookResult); valueRef.current = hookResult; useImperativeHandle(ref, () => ({ get current() { return valueRef.current; }, })); return null; }, ) as ( props: Props & {ref?: Ref>}, ) => ReactElement | null;