import { useRef , useMemo , useEffect } from 'react' ; /**
 * Keep the memorized value when it is  T * When the value change to undefined, the hook function keeps returning the memorized value
 * When the value change back to  T , the memorized value will be updated with the latest value
  export function usePreviousNonNullableValue < T > ( value ) { const prevValue = useRef < T | undefined | null > ( undefined ) ; const nonUndefinedValue = useMemo < T | null > ( ( ) => { if ( value !== undefined ) return value ; if ( prevValue . current !== undefined ) return prevValue . current ; return null ; } , [ value ] ) ; useEffect ( ( ) => { prevValue . current = nonUndefinedValue ; } , [ nonUndefinedValue ] ) ; return [ nonUndefinedValue , prevValue . current ] ; }