Consider the following scenario:
const ExampleComponent = () => {
const [status, setStatus] = useState(500)
//Calling this function with a click event
const submitHandler = () => {
const url = 'localhost:8000/foo'
fetch(url)
.then((res) => {
setStatus(res.status)
return res.json()
})
.then((res) => {
if (status === 200) {
console.log("OK")
} else {
console.log("Something went wrong")
}
})
}
}
In this case, since React queues the setStatus task, by the time I'm making the status === 200 check, the status has not been updated. So how do we work around such scenarios?