Created
March 13, 2025 11:17
-
-
Save whizkydee/cf0575c5bcf2eec2d473b9440241e11f to your computer and use it in GitHub Desktop.
Drop-in replacement for `useEffect` that helps debug unstable dependencies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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