Skip to content

Instantly share code, notes, and snippets.

@BolajiOlajide
Created April 16, 2020 00:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BolajiOlajide/7435b415793544ca463ecdb4be4b45f4 to your computer and use it in GitHub Desktop.
Save BolajiOlajide/7435b415793544ca463ecdb4be4b45f4 to your computer and use it in GitHub Desktop.
hook for tracing changed props
function useTraceUpdate(props) {
const prev = useRef(props);
useEffect(() => {
const changedProps = Object.entries(props).reduce((ps, [k, v]) => {
if (prev.current[k] !== v) {
ps[k] = [prev.current[k], v];
}
return ps;
}, {});
if (Object.keys(changedProps).length > 0) {
console.log('Changed props:', changedProps, '\n\n');
}
prev.current = props;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment