For event handlers inside the class component binding is required ( information from lots of sources) . But when console logging the 'this' keyword it logs 'context:undefined' ( because class body is strict as far I know) . And if I console log 'this' inside the event handler ( in this case the changeColor) it logs 'context:{...}' .
class Toggle extends React.Component {
constructor(props) {
super(props);
this.state = { color:green};
console.log(this); **// Toggle { ... context:undefined..... }**
this.changeColor = this.changeColor.bind(this);
}
changeColor() {
console.log(this); **// Toggle { ... context:{..} ..... }**
const newColor = this.state.color==green?yellow:green;
this.setState({color:newColor});
}
render() {
return (
<div style={{background:this.state.color}}>
<h1>
Change my color
</h1>
<button onClick={this.changeColor}>
Change color
</button>
</div>
);
}
}
If 'this' is undefined , what are we binding ?