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

179
Visualizações
Difference between passing event handler prop as arrow function or function reference

In a class Component, I have something like this:

render() {
  const { onClose } = this.props;

  // later, in the return statement, inside a bunch of other nested markup:
  <CloseIcon onClick={() => onClose()} className="qmCloseButton" />
  <AnotherIcon onClick={() => onClose("/path1")} className="qmCloseButton" />
  <AnotherIcon onClick={() => onClose("/path2")} className="qmCloseButton" />
}

The close handler prop that I pass in is:

const onCloseModal = urlAfterClose => {
  if (urlAfterClose) {
    history.push(urlAfterClose);
  } else {
    setShowSuccessModal(false);
  }
};

Everything works. However, I always like to avoid arrow functions in my markup when I can, so I try this small change:

  <CloseIcon onClick={onClose} className="qmCloseButton" />
  <AnotherIcon onClick={() => onClose("/path1")} className="qmCloseButton" />
  <AnotherIcon onClick={() => onClose("/path2")} className="qmCloseButton" />

But for the CloseIcon, the onClose method gets called with a class (looks like it might be the component itself?) instead of being called with no argument. It only works as an arrow function.

What am I missing here?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It's totally different syntaxes

onClick={onClose} =/= () => onClick={()=>onClose()}

onClick={onClose} in JavaScript this will pass all arguments available to onClose function, which second one runs without params

its same as

onClick={onClose} = onClick={(e)=>onClose(e)}
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