I am working on ES6 arrow functions in react. The 'this' keyword in the arrow function displays weirdly in the chrome debugger console. 'this' works fine in the current object context but when the value is checked in chrome debugger console, it shows incorrect value.
I already checked the How does the "this" keyword work? but that does not answer my question. My question is not about how this keyword is working but the weird value it displays in console.
In the debugger console, it shows as referring to the window but works correctly by pointing to the current instance.
I had no option but to create a video to demonstrate the issue. Would appreciate if some Javascript expert helps me resolve this.
Code:
import React, { Component } from 'react';
class App extends Component {
state = {name: "Rahul"}
nameChanger = () => {
debugger;
console.log("This is" + this.Window.name);
this.setState({name: "New Rahul"})
}
render() {
setTimeout(this.nameChanger, 1000);
return (
<div className="App">
{this.state.name}
</div>
);
}
}
export default App;