JustnGreen721
JustnGreen721

Reputation: 1

toFixed() causing an error with useState hooks?

In just making a static single-item purchasing modal, I've encountered a curious bug between the toFixed() and useState(). I wrote a basic changeQuantity(dir) function using a switch/case statement and when I try to use toFixed(2) on a tempTotal variable, I get an error telling me my useState (setQuantity) is now not a function?? The consequence is trivial and there's no issue just calling toFixed() on {total} in the UI, but I am curious as to whats causing it.

While I could see why using toFixed inside the setTotal() hook could cause fuss, I certainly wasn't expecting any lip out of setQuantity, so I'm curious how the hook seems to be overrode/not recognized?? Again, mainly asking out of curiosity!

`

  const changeQuant =(dir)=>{
    let temp = total;

    switch(dir){

      case "plus":
        setQuantity(quantity+1)
        // (temp + price).toFixed(2);
         setTotal(total+price)
        break;

        case "minus":
          if(quantity === 0) return;
        setQuantity(quantity-1)
        (temp - price).toFixed(2);
        // setTotal(temp)
        break;

      default:
        console.log("unknown action!")
    }
  }

`

Upvotes: 0

Views: 130

Answers (0)

Related Questions