I just started using React and I'm astounded at how incredibly difficult it is to do literally the most basic thing possible. All I want to do is make a request and display the response. Here's my code:
import React from 'react';
import 'whatwg-fetch';
export default class App extends React.Component {
async testBackend() {
let response = await fetch('http://localhost:8000/test', { credentials: 'include' });
return response.json();
}
render() {
let status = await this.testBackend();
return (
<div style={{ textAlign: 'center' }}>
<h1>Hello World</h1>
<p>{status}</p>
</div>
);
}
}
I can't use await in render() without making it async, but I can't make it aysnc because then it will return a Promise. I can't use then() in render() because it will also return a Promise. I can't store the result of the call in state because it won't be there by the time render() is called. So what do I do??
Why is this so hard? Any decent language would allow be to just block on the API call.