usePreviousDistinct<T> function

T? usePreviousDistinct<T>(
  1. T value, [
  2. Predicate<T>? compare
])

Just like usePrevious but it will only update once the value actually changes. This is important when other hooks are involved and you aren't just interested in the previous props version, but want to know the previous distinct value

Implementation

T? usePreviousDistinct<T>(T value, [Predicate<T>? compare]) {
  compare ??= (prev, next) => prev == next;
  final prevRef = useRef<T?>(null);
  final curRef = useRef<T>(value);
  final isFirstMount = useFirstMountState();

  if (!isFirstMount && !compare(curRef.value, value)) {
    prevRef.value = curRef.value;
    curRef.value = value;
  }

  return prevRef.value;
}