I'm quite new to hooks and in react in general. I came accross a problem I couldn't solve so here it is. I have a quotation function that is suppose to calculate the cost price of a benefit.
const quotation = useCallback(()=> {
setCostPrice(0)
keys.forEach(e => {
for (const property in values){
if ((property==e && values[property]==true){
setCostPrice(costPrice + pricing[property])
}
}
})
})
Here is my problem, i'm calling the function quotation() everytime a checkbox is checked (so the cost price will update). The problem is, the function is suppose to "reset" the costprice at 0 everytime it is called but since using "setCostPrice(0)" is asynchronous the result is not the right one. I've read different post concerning using useEffect() but when i'm using useEffect the function can't read the value from values[property].
I don't know how I can fix this issue if anyone wants to help I would appreciate !