Created
July 23, 2019 20:52
-
-
Save hanford/1f50906b4879e699d3528ba4d9ac3162 to your computer and use it in GitHub Desktop.
This file contains 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
// @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