import { useRef , useEffect } from "react" ; import shallowEqual from "shallowequal" ; const FIRST_RUN = { } ; /**
 * A function to compare two values to determine if they can be considered equal.
 */ export type Comparator < T > = ( a : T , b : T ) => boolean ; param value param comparator export default function useMemoValue < T > ( value , comparator = shallowEqual ) { let ref = useRef ( FIRST_RUN as T ) ; let nextValue = ref . current ; useEffect ( ( ) => { ref . current = nextValue ; } ) ; if ( ref . current === FIRST_RUN || ! comparator ( value , ref . current ) ) { nextValue = value ; } return nextValue ; }