teableio/teable

View on GitHub
packages/sdk/src/hooks/use-deep-compare-memoize.ts

Summary

Maintainability
A
0 mins
Test Coverage
import { dequal } from 'dequal';
import { useRef } from 'react';

type ISupportedValue = Record<string, unknown> | string | boolean | number | null;

export function useDeepCompareMemoize(value: ISupportedValue): ISupportedValue {
  const ref = useRef<ISupportedValue>(null);
  if (!dequal(value, ref.current)) {
    ref.current = value;
  }
  return ref.current;
}