2022-03-23 18:34:23 -04:00
|
|
|
import { useRef, useEffect } from "react"
|
2022-03-22 20:37:21 -04:00
|
|
|
|
|
|
|
function useTraceUpdate(props: { [key: string]: any }) {
|
2022-03-23 18:42:22 -04:00
|
|
|
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
|
|
|
|
}, {} as { [key: string]: any })
|
|
|
|
if (Object.keys(changedProps).length > 0) {
|
|
|
|
console.log("Changed props:", changedProps)
|
|
|
|
}
|
|
|
|
prev.current = props
|
|
|
|
})
|
2022-03-22 20:37:21 -04:00
|
|
|
}
|
|
|
|
|
2022-03-23 18:34:23 -04:00
|
|
|
export default useTraceUpdate
|