score:2

Accepted answer

setstate is asynchronous. console logging synchronously right after setting state to see what that state looks like afterwards will most likely not reflect a correct value. if you want to see the value when total changes, try something like this:

useeffect(() => {
  console.log(total);
}, [total]);

Related Query

More Query from same tag