Skip to content

Commit a3f5b96

Browse files
authored
Merge pull request #92 from Pistulls/master
Event handlers short hand example using arrow functions and avoid hav…
2 parents 80a205e + e547b70 commit a3f5b96

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

patterns/22.event-handlers.md

+19
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,22 @@ Arrow functions auto binds the function with `this`.
7575

7676
Facebook by the way recommend the same technique while dealing with functions that need the context of the same component.
7777
The binding in the constructor may be also useful if we pass callbacks down the tree.
78+
79+
A short hand example using arrow functions and avoid having to use the constructor:
80+
81+
```javascript
82+
class Switcher extends React.Component {
83+
state = { name: 'React in patterns' };
84+
85+
render() {
86+
return (
87+
<button onClick={ this._handleButtonClick }>
88+
click me
89+
</button>
90+
);
91+
}
92+
_handleButtonClick = () => {
93+
console.log(`Button is clicked inside ${ this.state.name }`);
94+
}
95+
}
96+
```

0 commit comments

Comments
 (0)