Skip to content

Instantly share code, notes, and snippets.

@nadilas
Created September 2, 2022 17:05
Show Gist options
  • Save nadilas/48f296410d3ea86ced6b69a2d61969bb to your computer and use it in GitHub Desktop.
Save nadilas/48f296410d3ea86ced6b69a2d61969bb to your computer and use it in GitHub Desktop.
react-trace-update-hook
export const 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.trace("Changed props:", changedProps)
}
prev.current = props
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment