import { useRef } from "react" ; import useWillUnmount from "@rooks/use-will-unmount" ; export default function useUnmountWithState < T extends any > ( state , callback ) { const stateRef = useRef < T > ( state ) ; stateRef . current = state ; useWillUnmount ( ( ) => { callback ( stateRef . current ) ; } ) ; }