useWhyDidYouUpdate
Usage
import { useEffect, useRef } from 'react';
type Props = Record<string, any>;
const useWhyDidYouUpdate = (name: string, props: Props) => {
const previousProps = useRef<Props>(props);
useEffect(() => {
if (previousProps.current !== props) {
const changedProps = Object.entries(props).reduce(
(acc: Record<string, [any, any]>, [key, value]) => {
if (previousProps.current[key] !== value) {
acc[key] = [previousProps.current[key], value];
}
return acc;
},
{}
);
if (Object.keys(changedProps).length) {
console.log(`[${name}] changed props:`, changedProps);
}
previousProps.current = props;
}
});
};
export default useWhyDidYouUpdate;Last updated