import { useCallback, useRef } from 'react' // A stable function that always runs the latest callback, so a child that calls it after the // parent re-rendered, or after the child itself was unmounted, still uses the current state export const useLatestCallback = , Result>( callback: (...args: Args) => Result ): ((...args: Args) => Result) => { const latest = useRef(callback) latest.current = callback return useCallback((...args: Args) => latest.current(...args), []) }