I'm curious why functionA returns an error while functionB has a correct reference to "this" and works. What is the reason for the difference?
class MyComponent extends Component {
...
render() {
return (
<input ref="myInput" type="text" />
<button onClick={this.functionA} />
<button onClick={this.functionB} />
);
}
}
This throws an error "Can't read property 'refs' of undefined:
functionA() {
const val = this.refs.myInput.value;
console.log(val);
}
While this correctly shows the value:
functionB = () => {
const val = this.refs.myInput.value;
console.log(val);
}