Created
July 17, 2023 09:20
-
-
Save sean-brydon/381776a44cf5940fee48a7d4c228ce99 to your computer and use it in GitHub Desktop.
Use trace update - react
This file contains hidden or 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
function useTraceUpdate(props: { [s: string]: unknown } | ArrayLike<unknown>) { | |
const prev = useRef(props); | |
useEffect(() => { | |
const changedProps = Object.entries(props).reduce((ps, [k, v]) => { | |
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | |
// @ts-ignore TODO: fix this | |
if (prev.current[k] !== v) { | |
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | |
// @ts-ignore TODO: fix this | |
ps[k] = [prev.current[k], v]; | |
} | |
return ps; | |
}, {}); | |
if (Object.keys(changedProps).length > 0) { | |
console.log("Changed props:", changedProps); | |
} | |
prev.current = props; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment