Skip to content

Instantly share code, notes, and snippets.

@vzaidman
Last active May 25, 2020 15:59
Show Gist options
  • Save vzaidman/02011e9d73c61a2c731d853ceea7c486 to your computer and use it in GitHub Desktop.
Save vzaidman/02011e9d73c61a2c731d853ceea7c486 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>
);
});
class Parent extends React.Component {
state = {count: 0}
// always the same function instance
handleClick = () => {
const {count} = this.state;
console.log(`hi #${count}!`);
}
render() {
return (
<>
<PureHeavyComponent onClick={this.handleClick} />
<button onClick={() => this.setState({count: this.state.count + 1})}>increase counter</button>
</>
);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment