File tree Expand file tree Collapse file tree 1 file changed +19
-0
lines changed Expand file tree Collapse file tree 1 file changed +19
-0
lines changed Original file line number Diff line number Diff line change @@ -75,3 +75,22 @@ Arrow functions auto binds the function with `this`.
75
75
76
76
Facebook by the way recommend the same technique while dealing with functions that need the context of the same component.
77
77
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
+ ```
You can’t perform that action at this time.
0 commit comments