Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

225
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda