DEV Community

Discussion on: Ways to Handle Deep Object Comparison in useEffect hook

Collapse
peculiarity profile image
John Doe

This is something I have implemented which works very well so far.

import { isEqual } from 'lodash'
import { useRef } from 'react'

export function useDeepEqualMemo<T>(value: T) {
  const ref = useRef<T | undefined>(undefined)

  if (!isEqual(ref.current, value)) {
    ref.current = value
  }

  return ref.current
}
Enter fullscreen mode Exit fullscreen mode