Last active
May 25, 2020 16:01
-
-
Save vzaidman/a6b0ee9944161ee2be190efee756b87e to your computer and use it in GitHub Desktop.
Advanced React- useCallback() Invalidates Too Often in Practice #14099
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
const PureHeavyComponent = React.memo(({onClick}) => { | |
console.log('pure heavy component!'); | |
return ( | |
<div onClick={onClick}> | |
//Many Other React elements are created here | |
</div> | |
); | |
}); | |
const Parent = () => { | |
console.log('parent!'); | |
// returns a new handleClick function on every render of Parent | |
const handleClick = React.useCallback(() => console.log('hi!'), []); | |
return ( | |
<PureHeavyComponent onClick={handleClick} /> | |
); | |
}; | |
// results: | |
// first render: | |
<Parent/> | |
// parent! | |
// pure heavy component! | |
// next renders: | |
<Parent/> | |
// parent! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment