// 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