Skip to content

Instantly share code, notes, and snippets.

@BerkeleyTrue
Created March 24, 2017 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BerkeleyTrue/d16ff84f8131f97393e2a37275fc5d02 to your computer and use it in GitHub Desktop.
Save BerkeleyTrue/d16ff84f8131f97393e2a37275fc5d02 to your computer and use it in GitHub Desktop.
export class BadFunComp extends React.PureComponent {
handleFun() {
const { funApi } = this.props;
funAPi();
}
render() {
// displayCoolness.props.handleFun will always have a new ref value
// causes child to re-render on ever BadFunComp render
return (
<DisplayCoolness onFun={ () => this.handleFun() } />
);
}
}
export class GoodFunComp extends React.PureComponent {
constructor(...args) {
super(...args);
// add bound method to instance
this.handleFun = this.handleFun.bind(this);
}
handleFun() {
const { funApi } = this.props;
funAPi();
}
render() {
// displayCoolness.props.handleFun always points to the same function
// displayCoolness does not needlessly re-render
return (
<div>
<DisplayCoolness onFun={ this.handleFun } />
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment