(value: T, isEqual: (a: T, b: T) => boolean)
| 15 | import {useRef} from 'react'; |
| 16 | |
| 17 | export function useDeepMemo<T>(value: T, isEqual: (a: T, b: T) => boolean): T { |
| 18 | // Using a ref during render is ok here because it's only an optimization – both values are equivalent. |
| 19 | // If a render is thrown away, it'll still work the same no matter if the next render is the same or not. |
| 20 | let lastValue = useRef<T | null>(null); |
| 21 | if (value && lastValue.current && isEqual(value, lastValue.current)) { |
| 22 | value = lastValue.current; |
| 23 | } |
| 24 | |
| 25 | lastValue.current = value; |
| 26 | return value; |
| 27 | } |
no test coverage detected