Created
October 5, 2018 07:19
-
-
Save sbpipb/8bb4b0b9247b7b02263731acb49c7aab to your computer and use it in GitHub Desktop.
Revisions
-
sbpipb revised this gist
Oct 5, 2018 . 1 changed file with 9 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ // I'll explain it again ```class MyComponent extends React.Component { @@ -11,8 +11,8 @@ I'll explain it again } }``` // This works // Why? Because you are passing a function as a value. It has not executed yet ```class MyComponent extends React.Component { myEvent = () => { console.log('do something') @@ -22,9 +22,9 @@ Why? Because you are passing a function as a value. It has not executed yet <button onClick={this.myEvent()}> WOW </button> } }``` // THIS does not work preoprly // Why? Because you invoked the function and passed its return to onClick // onClick is now `undefined` because there is nothing returned from this.myEvent's invocation ```class MyComponent extends React.Component { myEvent = () => { return () => console.log('do something') @@ -35,8 +35,8 @@ onClick is now `undefined` because there is nothing returned from this.myEvent's } }``` // THIS is weird but it will work // Why? Because myEvent returns a function that has not executed yet. (A thunk) ```class MyComponent extends React.Component { myEvent = (params) => { return () => console.log(params) @@ -47,4 +47,4 @@ Why? Because myEvent returns a function that has not executed yet. (A thunk) } }``` //This will work. It's a common pattern -
sbpipb created this gist
Oct 5, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,50 @@ I'll explain it again ```class MyComponent extends React.Component { myEvent = () => { console.log('do something') } render () { <button onClick={this.myEvent}> WOW </button> } }``` This works Why? Because you are passing a function as a value. It has not executed yet ```class MyComponent extends React.Component { myEvent = () => { console.log('do something') } render () { <button onClick={this.myEvent()}> WOW </button> } }``` THIS does not work preoprly Why? Because you invoked the function and passed its return to onClick onClick is now `undefined` because there is nothing returned from this.myEvent's invocation ```class MyComponent extends React.Component { myEvent = () => { return () => console.log('do something') } render () { <button onClick={this.myEvent()}> WOW </button> } }``` THIS is weird but it will work Why? Because myEvent returns a function that has not executed yet. (A thunk) ```class MyComponent extends React.Component { myEvent = (params) => { return () => console.log(params) } render () { <button onClick={(e) => this.myEvent(e)}> WOW </button> } }``` This will work. It's a common pattern