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

296
Vistas
onClick handler in React is triggered by adjacent element , not by element which it was meant to trigger
const ProductScreen = () => {
  
  const [qty, setQty] = useState(0);
  
  const handleAddtoCart = () => {
    console.log(qty);
  };

  return (
    <div className="productScreen">
            {product.countInStock > 0 && (
              <div className="productScreen__details__qty">
                <span>Qty : </span>
                <select
                  id="qty"
                  name="qty"
                  value={qty}
                  onChange={(e) => setQty(e.target.value)}
                >
                  {[...Array(product.countInStock).keys()].map((x) => (
                    <option key={x + 1} value={x + 1}>
                      {x + 1}
                    </option>
                  ))}
                </select>
              </div>
            )}
            {product.countInStock > 0 ? (
              <div className="productScreen__details__price__details__cart">
                <button
                  className="productScreen__details__price__details__toCart"
                  onClick={handleAddtoCart()}
                >
                  Add to Cart
                </button>
              </div>
          </div>
</div>
  );
};

Here handleAddtoCart gets triggered when selecting options but doesnt trigger when button is pressed(handleAddtoCart is added to button), when I change handleAddtoCart() to handleAddtoCart in onClick attribute of button it works properly.

Why when handleAddtoCart() is given as onclick attribute it is getting triggered by adjacent select option and is not getting triggered when button is pressed

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You need to make a callback to that function, because every render of the component, will literally execute handleAddtoCart() and not as you expect it to happen only of the onClick trigger. as react's Offical documents explained:

To save typing and avoid the confusing behavior of this, we will use the arrow function syntax for event handlers here and further below:

class Square extends React.Component {
 render() {
   return (
     <button className="square" onClick={() => console.log('click')}>
       {this.props.value}
     </button>
   );
 }
}

Notice how with onClick={() => console.log('click')}, we’re passing a function as the onClick prop. React will only call this function after a click. Forgetting () => and writing onClick={console.log('click')} is a common mistake, and would fire every time the component re-renders.

for more details: https://reactjs.org/tutorial/tutorial.html

about 4 years ago · Juan Pablo Isaza Denunciar

0

Change

onClick={handleAddtoCart()}

by

onClick={handleAddtoCart}

Also try with :

 onChange={(e) => setQty(e.currentTarget.value)}

instead of :

 onChange={(e) => setQty(e.target.value)}

The currentTarget read-only property of the Event interface identifies the current target for the event, as the event traverses the DOM. It always refers to the element to which the event handler has been attached, as opposed to Event.target, which identifies the element on which the event occurred and which may be its descendant.

https://developer.mozilla.org/en-US/docs/Web/API/Event/currentTarget

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