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

150
Visualizações
passing event handler as reference

this is an exercise from codecademy. Why does it require to pass the event handler as a reference? why i can't type onClick={goBack()} instead of onClick={goBack}

const goBack = () => {
    setQuestionIndex(prevQuestionIndex => prevQuestionIndex - 1);
  }

....

 <button onClick={goBack}>
          Go Back
 </button>
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

When you are using onClick={goBack()} then you are invoking/calling the function goBack and the result of goBack invocation get passed to onClick

Live Demo

Codesandbox Demo

But If you are using as onClick={goBack}, then In this case you are passing a reference to the onClick which means when user click on the button then It will automatically invoke the function. You don't have to invoke the function yourself.


1) Let say consider function with value

<button onClick={fun()}> button with function reference</button>

If you are passing value to a function then first of all it will produces a warning in the console as:

Warning:
Expected `onClick` listener to be a function, instead got a value of `string` type.

and when you invoke it then it will throw error as

Error
Expected `onClick` listener to be a function, instead got a value of `string` type.

2) If you are using as

<button onClick={fun}> button with function reference</button>

then function fun will be passed as a prop and when use click on the function then this function fun will get invoked automatically

about 4 years ago · Juan Pablo Isaza Relatório

0

goBack is a reference to a function, goBack() calls that function and returns the result.

In the case of the click handler for a button, of course, you want the function to run when the button is clicked, not when the button is rendered. That's why you need to pass the function, rather than calling it (on render) and passing its result.

about 4 years ago · Juan Pablo Isaza Relatório

0

If you pass in the function call instead of the reference, it will call the function as soon as your component renders, the reference is bound to the click handler, hence when the specific event triggers, it will callout to the reference.

Here is a snippet to see this, Sandbox

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