iamtheyammer

Improvements for TylerMcGinnis.com React Fundamentals

Feb 7th, 2019
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // implicit vs explicit returns
  2. () => ('abc') // returns 'abc'
  3. () => {'abc'} // returns undefined
  4.  
  5. // implicit vs explicit binding
  6. class Test extends React.Component {
  7.     // ...
  8.     constructor(props) {
  9.         super(props);
  10.         this.myOtherMethod = this.myOtherMethod.bind(this);
  11.     }
  12.     componentDidMount() { // react binds this
  13.         console.log(this) // returns the Test this
  14.     }
  15.     myMethod = () => { // implicit binding
  16.         console.log(this); // returns the Test this
  17.     }
  18.     myOtherMethod() { // explicitly bound in the constructor method
  19.         console.log(this); // returns the Test this
  20.     }
  21.     myThirdMethod() { // no binding
  22.         console.log(this); // returns the this from where it was called (left of the dot)
  23.     }
  24.     // ...
  25. }
  26.  
  27. /*
  28.     License:
  29.     CC0 @iamtheyammer
  30.     Feel free to use this code in any demo, commercial or non-commercial, with or without credit.
  31. */
Advertisement
Add Comment
Please, Sign In to add comment