iamtheyammer icon

Improvements for TylerMcGinnis.com React Fundamentals

iamtheyammer | PRO | 02/07/19 08:08:28 PM UTC | 0 ⭐ | 304 👁️ | Never ⏰ | []
JavaScript |

830 B

|

None

|

0 👍

/

0 👎

// implicit vs explicit returns
() => ('abc') // returns 'abc'
() => {'abc'} // returns undefined
 
// implicit vs explicit binding
class Test extends React.Component {
    // ...
    constructor(props) {
        super(props);
        this.myOtherMethod = this.myOtherMethod.bind(this);
    }
    componentDidMount() { // react binds this
        console.log(this) // returns the Test this
    }
    myMethod = () => { // implicit binding
        console.log(this); // returns the Test this
    }
    myOtherMethod() { // explicitly bound in the constructor method
        console.log(this); // returns the Test this
    }
    myThirdMethod() { // no binding
        console.log(this); // returns the this from where it was called (left of the dot)
    }
    // ...
}
 
/*
    License:
    CC0 @iamtheyammer
    Feel free to use this code in any demo, commercial or non-commercial, with or without credit.
*/

Comments