Skip to content

Instantly share code, notes, and snippets.

@sean-brydon
Created July 17, 2023 09:20
Show Gist options
  • Save sean-brydon/381776a44cf5940fee48a7d4c228ce99 to your computer and use it in GitHub Desktop.
Save sean-brydon/381776a44cf5940fee48a7d4c228ce99 to your computer and use it in GitHub Desktop.
Use trace update - react
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