Last active
June 10, 2018 19:30
-
-
Save dhruvdutt/7a0fc3cca6205dd591fbc1eb12ebe3e3 to your computer and use it in GitHub Desktop.
React - optimise creation of new functions in render
This file contains hidden or 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
class SayHi extends Component { | |
// We can't use showMessage directly because it creates | |
// a new event handler every time you call it. | |
showMessage = (msg) => (e) => { | |
console.log(`Say ${msg}`, e) | |
} | |
// So instead, we need to create the event handlers beforehand, | |
// then call those inside of `render`. | |
showHiMessage = this.showMessage('Hi') | |
render () { | |
return ( | |
<Button onClick={this.showHiMessage}> | |
Click Me | |
</Button> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment