Created
March 24, 2017 17:19
-
-
Save BerkeleyTrue/d16ff84f8131f97393e2a37275fc5d02 to your computer and use it in GitHub Desktop.
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
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