When I press Ctrl + S in VSCode, my code with two API calls inside a useEffect hook works fine. However, when I reload my web browser (Safari), only one of the API calls finishes. Why does this occur? I get no error response or something that indicates that there is a problem. Have I misunderstand how useEffect works?
import React, { useState, useContext, useEffect } from "react"
import { AccountContext } from "../../context"
....
export default function GradingAfter() {
const context = useContext(AccountContext)
const { token, userId } = context
const [grading, setGrading] = useState([])
const [beltInfo, setBeltInfo] = useState({
belt_name: "",
color: "" // Assuming color is directly usabl
})
const grading_id = 7
useEffect(() => {
async function fetchData() {
try{
const grading_response = await fetch(`/api/examination/grading/${grading_id}`, {
method: "GET",
headers: { token }
})
if (!grading_response.ok) {
throw new Error("Network response was not ok")
}
const grading_data = await grading_response.json()
setGrading(grading_data)
const belt_response = await fetch("/api/belts/all", {
method: "GET",
headers: { token }
})
if (!belt_response.ok) {
throw new Error("Network response was not ok")
}
const belt_data = await belt_response.json()
console.log("Belt data:", belt_data)
} catch (error) {
console.error("There was a problem with the fetch operation:", error)
}
}
fetchData()
},[])
return(...)
2
Answers
This implementation solved my problems.