import { useState , useEffect } from "react" ; import { useTimeoutFn } from "react-use" ; export default < T > ( baseState , ms = 1000 ) : [ T , ( transientValue : T ) => void ] => { const [ value , setValue ] = useState < T > ( baseState ) ; const [ , cancel , restart ] = useTimeoutFn ( ( ) => setValue ( baseState ) , ms ) ; useEffect ( ( ) => { cancel ( ) ; setValue ( baseState ) ; } , [ baseState , ms ] ) ; const setTransientValue = ( transientValue ) : void => { setValue ( transientValue ) ; restart ( ) ; } ; return [ value , setTransientValue ] ; } ;