Skip to content

Instantly share code, notes, and snippets.

@jeffellis
Created September 27, 2021 17:19
Show Gist options
  • Save jeffellis/09378d0784951d0747aadc848d9d1dcc to your computer and use it in GitHub Desktop.
Save jeffellis/09378d0784951d0747aadc848d9d1dcc to your computer and use it in GitHub Desktop.
Hook for logging prop diffs in a functional component
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