Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

223
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda