import { useEffect , useRef , useState } from 'react' ; export function useStateDelay ( state , delay , callback ? ) { const [ delayedState , setState ] = useState ( false ) ; const callbackRef = useRef ( callback ) ; callbackRef . current = callback ; useEffect ( ( ) => { if ( state === delayedState ) { return ; } const timerId = setTimeout ( ( ) => { setState ( state ) ; if ( state ) { callbackRef . current ?. ( ) ; } } , delay ) ; return ( ) => clearTimeout ( timerId ) ; } , [ state , delayedState , delay ] ) ; return delayedState ; }