Created
September 27, 2021 17:19
-
-
Save jeffellis/09378d0784951d0747aadc848d9d1dcc to your computer and use it in GitHub Desktop.
Hook for logging prop diffs in a functional component
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 { useEffect, useRef } from 'react'; | |
const useLogPropDiffs = (props, label = '') => { | |
const previousProps = useRef({}); | |
useEffect(() => { | |
previousProps.current = props; | |
}); | |
if (previousProps.current) { | |
for (const [key, value] of Object.entries(props)) { | |
if (value !== previousProps.current[key]) { | |
console.log(`${label} prop differs ${key}: ${value?.toString()} ${previousProps.current[key]?.toString()}`); | |
} | |
} | |
} | |
previousProps.current = props; | |
} | |
export default useLogPropDiffs; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment