Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

227
Views
Counter going above abviable stock and below initial values

I am burning out here so I may be missing something, why is it not following the parameters?

const addItem = () => {
    const newValue = setCounter(counter +1);
       
    if (newValue < {stock}){
        setCounter(newValue)
    } else if (newValue > {stock}) {
        alert("Stock is full");
        setCounter({stock})
    }
}

const quitItem = () => {
    const newValue = setCounter(counter -1)
    if (newValue < initial){
        setCounter(initial)
    }

}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Whenever you call setCounter, you are actually updating its value in React state. So in the first line inside addItem you have already updated the value of the counter.

The next thing is that you inside addItem is comparing newValue to an object with the variable stock. You should probably compare the value against the value of stock, not an object containing stock.

Try this:

const addItem = () => {
    const newValue = counter + 1;
       
    if (newValue < stock){
        setCounter(newValue)
    } else if (newValue > stock) {
        alert("Stock is full");
        setCounter(stock)
    }
}

const quitItem = () => {
    const newValue = counter -1
    if (newValue < initial){
        setCounter(initial)
    }

}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!