Skip to content

Instantly share code, notes, and snippets.

@gabrielpetersson
Created February 11, 2022 12:43
Show Gist options
  • Save gabrielpetersson/0e4f3477c81065e979cd9b982b73e0d7 to your computer and use it in GitHub Desktop.
Save gabrielpetersson/0e4f3477c81065e979cd9b982b73e0d7 to your computer and use it in GitHub Desktop.
import { log } from "../logging";
import { usePrevious } from "./usePrevious";
// NOTE(gab): Put dependencies in an object & see which deps makes
// components rerender
export const useDependencyDebugger = (
dependencies: Record<string, unknown>
): void => {
const dependencyValues = Object.values(dependencies);
const dependencyNames = Object.keys(dependencies);
const previousDeps = usePrevious(dependencyValues);
const changedDeps = dependencyValues.reduce(
(accum: Record<string, unknown>, dependency: unknown, index: number) => {
if (previousDeps != null && dependency !== previousDeps[index]) {
const dependencyName = dependencyNames[index]!;
return {
...accum,
[dependencyName]: {
before: previousDeps[index],
after: dependency,
},
};
}
return accum;
},
{}
);
if (Object.keys(changedDeps).length > 0) {
log.info("[use-effect-debugger] ", changedDeps);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment