Skip to content

Instantly share code, notes, and snippets.

@dhruvdutt
Last active June 10, 2018 19:30
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 dhruvdutt/7a0fc3cca6205dd591fbc1eb12ebe3e3 to your computer and use it in GitHub Desktop.
Save dhruvdutt/7a0fc3cca6205dd591fbc1eb12ebe3e3 to your computer and use it in GitHub Desktop.
React - optimise creation of new functions in render
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