I have seen different variations of handling states in ReactJS tutorials.
I have seen the following:
class App extends Component {
constructor () {
super()
this.state = {
message: null,
};
}
componentDidMount() {
fetch('/api/hello')
.then(response => response.json())
.then(message => this.setState({ message }));
}
}
and
class App extends Component {
state = {};
hello = () => {
fetch("/api/hello")
.then(response => response.text())
.then(message => this.setState({ message }));
};
}
Both behave as expected, i.e. it was able to render the message from /api/hello
. I am wondering the difference between the two where one is setting the state in the constructor and the other isn't.