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

141
Visualizações
Need to understand inline function call with react FC

Im quite new to React and have created a new view at my company. I use inline functions because many guides do the same. Everything is written as function components.

I write onClick like this:

onClick: () => onBackClick()

My co-workes says this is a usless arrow function.

Will try to give you the whole picture. Parent component has the onBackClick function declared

const Parent = () => {
     const onBackClick = () => {backOff}
    
          <Child {...{onBackClick}}/>

}

const Child = ({onBackClick}) => {
 return (
   <MyButton {...{onClick: () => onBackClick() }}/>
)

}

My co-workers want me to write it like this:

onClick: onBackClick

I dont get a good explanation on why and really would like to learn. Can someone be kind to explain why this is a better way to write it.

Regards.

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

0

Compare:

const onBackClick = () => { console.log("This does something useful"); };

const onClick = onBackClick;

onClick();

const onBackClick = () => { console.log("This does something useful"); };

const onClick = () => onBackClick();

onClick();

In the first example (your co-worker's way) there is one function and it is passed around.

In the second example (your way) you create a whole new function that does nothing except call the first function.


Aside from being redundant, this creates an extra inefficiency in React.

When the component is re-rendered, the output is checked for changes.

onBackClick might not have changed, but (when using your way) onClick has, so React will delete the old event handler from the DOM and assign a new one with the new (but identical) onClick function you created.

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