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

107
Visualizações
best way to have more than one function for react onClick events

I am working on a react button component that receives 1 function as prop to be executed on the onClick event

<Button onClick={() => console.log('hello')}>

this passes as

<button onClick={onClick}>
  Say hello
</button>

A new requirement has come, and we need to make a ping to our server to see how many users actually click on the button, but not every button will share this behaviour, what would be a clean way to add a new property and if it is present, execute both functions on click

<Button
    onClick={() => console.log('hello')}
    track=true
/>
<button
  onClick={() =>{
     if track trackClick()
     onClick()
  }}>
  Say hello
</button>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Shift everything into the parent component. Have a state that logs the counts of the buttons, and have your handleClick function call the first function and then, depending on the track information, update the count. Button1, in this example, will call doThing first and then update the count; Button2 will only call the function, and not update the count.

const { useEffect, useState } = React;

function Example() {

  const [ count, setCount ] = useState(0);

  useEffect(() => console.log(count), [count]);

  function doThing() {
    console.log('Thing');
  }

  function handleClick(e) {
    const { track } = e.target.dataset;
    doThing();
    if (track === 'true') setCount(count + 1);
  }

  return (
    <div>
      <Button
        handleClick={handleClick}
        text="Button1"
        track={true}
      />
      <Button
        handleClick={handleClick}
        text="Button2"
        track={false}
      />
    </div>
  );

}

function Button({ handleClick, text, track }) {
  return (
    <button
      onClick={handleClick}
      data-track={track}
    >{text}
    </button>
  );
}

ReactDOM.render(
  <Example />,
  document.getElementById('react')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>
<div id="react"></div>

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