Created
October 25, 2021 13:36
-
-
Save davemecha/59d843623d9e288028c433b67050b41f to your computer and use it in GitHub Desktop.
Helper hook to memoize params for deep comparison in useEffect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { isEqual } from 'lodash'; | |
import { useRef } from 'react'; | |
/** | |
* usage: | |
* `useEffect(() => { ... }, [useDeepCompareMemoize(param)]);` | |
*/ | |
export default function useDeepCompareMemoize<T>(value: T) { | |
const ref = useRef<T>(); | |
if (!isEqual(value, ref.current)) ref.current = value; | |
return ref.current; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment