Skip to content

Instantly share code, notes, and snippets.

@hanford
Created July 23, 2019 20:52
Show Gist options
  • Save hanford/1f50906b4879e699d3528ba4d9ac3162 to your computer and use it in GitHub Desktop.
Save hanford/1f50906b4879e699d3528ba4d9ac3162 to your computer and use it in GitHub Desktop.
// @flow
import * as React from 'react';
// inside functional components, you can drop this hook
// and be notified when the modified component re-renders.
// this component will also tell you _why_ it was re-rendered
export default function useTraceUpdate(props: any) {
const prev = React.useRef(props);
React.useEffect(() => {
if (process.env.NODE_ENV !== 'production') {
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) {
// eslint-disable-next-line
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