import { useEffect, useRef } from "react"; import useRenderCount from "@hooks/useRenderCount"; type ChangedProps = Record; type DebugInformationResult = { count: number; changedProps: ChangedProps; timeSinceLastRender: number; lastRenderTimestamp: number; }; export default function useDebugInformation( componentName: string, props: Record ): DebugInformationResult { const count = useRenderCount(); const changedProps = useRef({}); const previousProps = useRef(props); const lastRenderTimestamp = useRef(Date.now()); const propKeys = Object.keys({ ...props, ...previousProps.current }); 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: DebugInformationResult = { 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; }