Skip to content

Instantly share code, notes, and snippets.

@andyshora
Last active December 2, 2020 10:07
Show Gist options
  • Save andyshora/6e5b03a57302722178576ec61ab002c3 to your computer and use it in GitHub Desktop.
Save andyshora/6e5b03a57302722178576ec61ab002c3 to your computer and use it in GitHub Desktop.
Debug React re-renders
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);
}
prev.current = props;
});
}
// usage
function MyComponent(props) {
useTraceUpdate(props)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment