I'm calling setState function so many time in a row (by pressing down button). but problem is react js stop updating state after some updating(40-50 times). I'm increasing sold value by one on each click.
I want to know is it the normal behaviour of react js? or I'm missing something?
axios.put(`http://localhost:5000/soldUpdate`,item)
.then(res => {
const {quantity,sold,...rest} = item;
const newQuantity = parseInt(quantity)-1;
const newSold = parseInt(sold)+1;
if(res.status===200){
setItem({...rest,quantity:newQuantity,sold:newSold})
} else {
console.log("not updated!");
}
After too many clicks in a row, the API you're calling may block your request. In that case, you won't get any response, but an error so .then()
isn't going to be called. That's probably the reason why your state isn't changing. The best way to check if the API responds with an error is by .catch()
.
Also, check the official Axios docs on handling errors.