Created
March 27, 2019 17:20
-
-
Save conor909/88cc83a8dad8ac7dc50e8a50ca961395 to your computer and use it in GitHub Desktop.
Handy hook to find out what caused a rerender
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 { useEffect, useRef } from 'react'; | |
export default function whatChanged(props) { | |
const prev = useRef(props); | |
useEffect(() => { | |
const changedProps = Object.entries(props).reduce((ps, [k, v]) => { | |
if (prev.current[k] !== v) { | |
ps[k] = [prev.current[k], v]; | |
} | |
return ps; | |
}, {}); | |
if (Object.keys(changedProps).length > 0) { | |
console.log('Changed props:', changedProps); | |
} | |
prev.current = props; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment