-
Notifications
You must be signed in to change notification settings - Fork 126
/
useDebugInformation.js
32 lines (28 loc) · 1.05 KB
/
useDebugInformation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { useEffect, useRef } from "react"
import useRenderCount from "../useRenderCount/useRenderCount"
export default function useDebugInformation(componentName, props) {
const count = useRenderCount()
const changedProps = useRef({})
const previousProps = useRef(props)
const lastRenderTimestamp = useRef(Date.now())
const propKeys = Object.keys({ ...props, ...previousProps })
changedProps.current = propKeys.reduce((obj, key) => {
if (props[key] === previousProps.current[key]) return obj
return {
...obj,
[key]: { previous: previousProps.current[key], current: props[key] },
}
}, {})
const info = {
count,
changedProps: changedProps.current,
timeSinceLastRender: Date.now() - lastRenderTimestamp.current,
lastRenderTimestamp: lastRenderTimestamp.current,
}
useEffect(() => {
previousProps.current = props
lastRenderTimestamp.current = Date.now()
console.log("[debug-info]", componentName, info)
})
return info
}