Last active
February 19, 2020 10:17
-
-
Save compulim/d0ae148df5245c3ab1800c77ef9331cf to your computer and use it in GitHub Desktop.
Track changes on every render via React hooks
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
import { useRef } from 'react'; | |
export default function useDebugDeps(depsMap, name) { | |
const lastDepsMapRef = useRef({}); | |
const { current: lastDepsMap } = lastDepsMapRef; | |
const keys = new Set([...Object.keys(depsMap), ...Object.keys(lastDepsMap)]); | |
const keysChanged = Array.from(keys).filter(key => !Object.is(depsMap[key], lastDepsMap[key])); | |
if (keysChanged.length) { | |
console.groupCollapsed(`Changes found in ${ name }`); | |
console.log(keysChanged); | |
console.groupEnd(); | |
} | |
lastDepsMapRef.current = depsMap; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment