30 lines
783 B
JavaScript
30 lines
783 B
JavaScript
'use client'
|
|
|
|
// src/use-why-update.ts
|
|
import { useEffect, useRef } from "react";
|
|
function useWhyDidYouUpdate(name, props) {
|
|
const previousProps = useRef();
|
|
useEffect(() => {
|
|
if (previousProps.current) {
|
|
const allKeys = Object.keys({ ...previousProps.current, ...props });
|
|
const changesObj = {};
|
|
allKeys.forEach((key) => {
|
|
if (previousProps.current[key] !== props[key]) {
|
|
changesObj[key] = {
|
|
from: previousProps.current[key],
|
|
to: props[key]
|
|
};
|
|
}
|
|
});
|
|
if (Object.keys(changesObj).length) {
|
|
console.log("[why-did-you-update]", name, changesObj);
|
|
}
|
|
}
|
|
previousProps.current = props;
|
|
});
|
|
}
|
|
|
|
export {
|
|
useWhyDidYouUpdate
|
|
};
|
|
//# sourceMappingURL=chunk-W4SX2OS5.mjs.map
|