Skip to content

Instantly share code, notes, and snippets.

@litera
Created April 1, 2025 06:50
Show Gist options
  • Save litera/8b520cbe83cc245794f15626efece65a to your computer and use it in GitHub Desktop.
Save litera/8b520cbe83cc245794f15626efece65a to your computer and use it in GitHub Desktop.
Simplified value change trace hook for debugging purposes
const useTraceUpdate = <T extends Record<string, unknown>>(props: T) => {
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 Record<string, unknown>
);
if (Object.keys(changedProps).length > 0) {
console.log('Changed props:', changedProps);
}
prev.current = props;
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment