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

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

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 Relatório

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 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