Skip to content

Instantly share code, notes, and snippets.

@nesbtesh
Created October 13, 2023 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nesbtesh/a578fa264f13fb0a8539452920dfe90a to your computer and use it in GitHub Desktop.
Save nesbtesh/a578fa264f13fb0a8539452920dfe90a to your computer and use it in GitHub Desktop.
useTraceUpdate
import React, { useEffect, useRef } from "react";
export default function useTraceUpdate(props) {
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;
}, {});
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