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

330
Visualizações
React Native component callback functions

In components, I've seen different way of doing callbacks. What is the difference between:

<MyButton onPress={ () => {doSomething(data)} }>

and

<MyButton onPress={ this.doSomething.bind(this) }>
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

<MyButton onPress={ () => {doSomething(data)} }>

This code block uses the ES6 Arrow function; which is another way of declaring a function in javascript. Also, the scope of this in arrow function depend where the function was created as opposed to normal scoping rule of this which by default depends on how the function was called.

<MyButton onPress={ this.doSomething.bind(this) }>

This statement makes a call to doSomething method. But since the event registration is done on different element, the Scope of doSomething is different and is forcefully binded by using bind method in javascript.

Also, in the second method you are not passing the data parameter, which you can pass using the second argument to the method like shown below.

<MyButton onPress={ this.doSomething.bind(this, data)} }>

over 4 years ago · Santiago Trujillo Relatório

0

<MyButton onPress={ () => {doSomething(data)} }>

Is calling a new anonymous function onPress, which will run doSomething.

<MyButton onPress={ this.doSomething.bind(this) }>

Is calling a reference to a named function, that you have already defined in your class. The binding to this is required only when you are working with class function (I mean, non ES6 arrow function).

const doSomething = () => { ... }

Would not require a .bind(this), since arrow functions bind on lexical scope.

You should definately read over What is the best and most efficient way to bind callbacks in ReactJS? In the constructor or in the render method or as a property initializer?

over 4 years ago · Santiago Trujillo 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