I have the following code which is working as expected:
import React, { Component } from 'react';
let result = null;
class MyData extends Component {
_getData = () => {
fetch(url)
.then(response => response.json())
.then(data => {
result = items.find(
item => item.id === 'abcd'
);
});
};
componentDidMount() {
this._getData();
}
render() {
return result ? <span>hello</span> : null;
}
}
When I try to move result as an instance variable of the class as follows, it always remains null in the render() method. Though the assignment is working correctly post fetch:
import React, { Component } from 'react';
class MyData extends Component {
result = null;
_getData = () => {
fetch(url)
.then(response => response.json())
.then(data => {
this.result = items.find(
item => item.id === 'abcd'
);
});
};
componentDidMount() {
this._getData();
}
render() {
return this.result ? <span>hello</span> : null;
}
}
Can someone explain me what is the expected usage here. And how things have changed from ES6 onwards.

=>functions don't dothisthe way that regular functions do, so you've got no reference to the instance in the_getDatafunction.=>class property function results inthisbeing the class instance inside the functionthisvalue bound to the new instance. That makes sense, though it's part of why instance property syntax really makes me upset. It's a "me" problem of course.