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

164
Vistas
How do I listen for a number value in reactjs

I have made a shopping cart in react. I have assigned a value to each button. The value shows in the dom so I know it's there.

I created a function to capture the value and then push it into an array. However, when I hit add to cart it say's "nothing added". I've been trying to find a way to listen for the value in the event listener but nothing seems to work.

I have tried putting just e.target.value but the outcome is the same.

Any ideas?

const [item, setItem] = useState([]);

function addCart(e) {
    if (e.target.value === "") {
      item.push(e.target.value);
      console.log(item);
      setCart(cart + 1);
    } else {
      console.log("nothing added");
    }
  }

 <button
              value={pro.price}
              onClick={addCart}
              className="bg-blue-500 text-white font-bold border-white p-2 rounded-md"
            >
              {"add to cart"}
            </button>
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

I think you need to negate the condition in the if block. e.target.value !== ""

about 4 years ago · Juan Pablo Isaza Denunciar

0

If you want to update your state item, you've to use setItem.

function addCart(e) {
    if (e.target.value !== "") {
      setItem([...item, e.target.value]);
      console.log(item);
      setCart(cart + 1);
    } else {
      console.log("nothing added");
    }
  }
about 4 years ago · Juan Pablo Isaza Denunciar

0

I suggest that you could change your approach and use a class instead of a function as such :

class AddCard extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      value: 'VALUE'
    }
  }
  handleChange (e) {
    console.log('handleChange called')
  }
  handleClick () {
    this.setState({value: 'UPDATED_VALUE'})
    var event = new Event('input', { bubbles: true });
    this.myinput.dispatchEvent(event);
  }
  render () {
    return (
      <div>
        <input readOnly value={this.state.value} onChange={(e) => {this.handleChange(e)}} ref={(input)=> this.myinput = input}/>
        <button onClick={this.handleClick.bind(this)}>Change Input</button>
      </div>
    )
  }
}

ReactDOM.render(<AddCard />,  document.getElementById('app'))

By doing so you'll be able to dissociate the click event and the change event

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