Skip to content

Instantly share code, notes, and snippets.

@whizkydee
Created March 13, 2025 11:17
Show Gist options
  • Save whizkydee/cf0575c5bcf2eec2d473b9440241e11f to your computer and use it in GitHub Desktop.
Save whizkydee/cf0575c5bcf2eec2d473b9440241e11f to your computer and use it in GitHub Desktop.
Drop-in replacement for `useEffect` that helps debug unstable dependencies
const useEffectWithChangeLogger = (
effect: () => void,
deps: any[],
name = 'Component',
) => {
const previousDepsRef = useRef<any[] | null>(null);
useEffect(() => {
if (previousDepsRef.current) {
const changedDeps = deps
.map((dep, index) => ({
index,
prev: previousDepsRef.current?.[index],
next: dep,
}))
.filter(({prev, next}) => prev !== next);
if (changedDeps.length > 0) {
console.log(`[${name}] Dependencies changed:`, changedDeps);
}
}
previousDepsRef.current = deps;
return effect();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, deps);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment