Questions
Answer the following questions to check what you have learned in this chapter:
- The following effect attempts to fetch data from a REST API and store it in the state:
useEffect(async () => {
const response = await fetch('https://some-rest-api/');
const data = await response.json();
setData(data);
}, []);
What are the problems with this implementation?
- The following fetching function returns an array of first names:
export async function getFirstNames() {
const response = await fetch('https://some- firstnames/');
const body = await response.json();
return body;
}
However, the return type of the function is any
. So, how can we improve the implementation to have a return type of string[]
?
- In the
fetch
function argument, what should be specified in themethod
option for it to make an HTTPPUT
request?fetch(url, {
method: ???,
body...