Skip to content

Instantly share code, notes, and snippets.

@jondeandres
Created March 27, 2019 23:42
Show Gist options
  • Save jondeandres/7cb5eb797aaffe90964c8b1ad6eb713f to your computer and use it in GitHub Desktop.
Save jondeandres/7cb5eb797aaffe90964c8b1ad6eb713f to your computer and use it in GitHub Desktop.
/**
* Can you explain the differences between all those ways
* of passing function to a component?
*
* What happens when you click each of the buttons?
*/
class App extends React.Component {
constructor() {
super();
this.name = 'MyComponent';
this.handleClick2 = this.handleClick1.bind(this);
}
handleClick1() {
alert(this.name);
}
handleClick3 = () => alert(this.name);
render() {
return (
<div>
<button onClick={this.handleClick1()}>click 1</button>
<button onClick={this.handleClick1}>click 2</button>
<button onClick={this.handleClick2}>click 3</button>
<button onClick={this.handleClick3}>click 4</button>
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment