Skip to content

Instantly share code, notes, and snippets.

@latobibor
Last active October 29, 2025 19:43
Show Gist options
  • Select an option

  • Save latobibor/8d347ae805b848b12a5d7280c3f8dd72 to your computer and use it in GitHub Desktop.

Select an option

Save latobibor/8d347ae805b848b12a5d7280c3f8dd72 to your computer and use it in GitHub Desktop.
React dependency spy
/**
* Ever wondered what triggered a rerendering in React?
* Who was the killer? Which dependency got updated?
* This is a very stupid little code that can help you debug the issue.
*/
import { useRef } from 'react';
type DependenciesObject = {
[dependencyName: string]: unknown;
};
export function useDependencySpy(dependenciesObject: DependenciesObject) {
const previousDependenciesRef = useRef<DependenciesObject>({});
Object.keys(dependenciesObject).forEach(key => {
if (!Object.is(previousDependenciesRef.current[key], dependenciesObject[key])) {
console.info(`[${key}] variable reference has changed!`);
previousDependenciesRef.current[key] = dependenciesObject[key];
}
});
}
// usage:
function MyComponent() {
const memoedThingThatShouldNotChange = useMemo(() => computeSomething(a, b), [a, b]);
useDependencySpy({ a, b });
// just a random UI
return <div>JSON.stringify(memoedThingThatShouldNotChange)</div>;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment