import { useEffect , useState } from 'react' ; param value param filterFn export default function useLast < T > ( value , filterFn ? ) { const [ last , setLast ] = useState < T | null | undefined > ( filterFn && filterFn ( value ) ? value : undefined ) ; useEffect ( ( ) => { setLast ( ( prev ) => { const shouldUse = filterFn ? filterFn ( value ) : true ; if ( shouldUse ) return value ; return prev ; } ) ; } , [ filterFn , value ] ) ; return last ; } function isDefined < T > ( x ) { return x !== null && x !== undefined ; } param value export function useLastTruthy < T > ( value ) { return useLast ( value , isDefined ) ; }