Skip to content

Instantly share code, notes, and snippets.

@vzaidman
Last active May 25, 2020 16:01
Show Gist options
  • Save vzaidman/a6b0ee9944161ee2be190efee756b87e to your computer and use it in GitHub Desktop.
Save vzaidman/a6b0ee9944161ee2be190efee756b87e to your computer and use it in GitHub Desktop.
Advanced React- useCallback() Invalidates Too Often in Practice #14099
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