Created
February 11, 2022 12:43
-
-
Save gabrielpetersson/0e4f3477c81065e979cd9b982b73e0d7 to your computer and use it in GitHub Desktop.
This file contains 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
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