I have a quick question about how React changes a function component sate using hooks. I am fetching some data from ASW RDS MySQL database. The relevant piece of code looks as follows:
const [data, upDateData] = React.useState([]);
.....
async function load() {
let response = await fetch("/api/employee");
let body = await response.json();
upDateData(body);
console.log(body);
console.log(data)
}
My question is the following. When I console log the "body" variable which is the actual response, I get an array of Json objects, exactly what I expect. However, when I do the same with my "data" variable defined in the hook, I get an empty array even though I console log after the updateData function has been called and the value of "data" has (presumably) been changed. Could somebody explain why that is? I suspect that this may have something to do with async nature of the function but I am not sure. Thank you in advance!