Skip to content

Commit

Permalink
Merge pull request vasanthk#92 from Pistulls/master
Browse files Browse the repository at this point in the history
Event handlers short hand example using arrow functions and avoid hav…
  • Loading branch information
vasanthk authored Nov 18, 2017
2 parents 80a205e + e547b70 commit a3f5b96
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions patterns/22.event-handlers.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,22 @@ Arrow functions auto binds the function with `this`.

Facebook by the way recommend the same technique while dealing with functions that need the context of the same component.
The binding in the constructor may be also useful if we pass callbacks down the tree.

A short hand example using arrow functions and avoid having to use the constructor:

```javascript
class Switcher extends React.Component {
state = { name: 'React in patterns' };

render() {
return (
<button onClick={ this._handleButtonClick }>
click me
</button>
);
}
_handleButtonClick = () => {
console.log(`Button is clicked inside ${ this.state.name }`);
}
}
```

0 comments on commit a3f5b96

Please sign in to comment.