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

288
Visualizações
Why we have to bind a method in Class Component React

I was reading the documentation and practicing some things in the React documentation until I finally entered the event handling section. but I don't understand why when using method in class component we have to bind the function, can anyone explain it? for examples :

class Toggle extends React.Component {
  constructor(props) {
    super(props);
    this.state = {isToggleOn: true};

    // This binding is necessary to make `this` work in the callback
    this.handleClick = this.handleClick.bind(this);
  }

  handleClick() {
    this.setState(prevState => ({
      isToggleOn: !prevState.isToggleOn
    }));
  }

  render() {
    return (
      <button onClick={this.handleClick}>
        {this.state.isToggleOn ? 'ON' : 'OFF'}
      </button>
    );
  }
}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

The point is to keep correct value of this reference. Check this example:

class Example {
  private prop = 1;

  echoProp() {
    console.log(this?.prop);
  }
}

const example = new Example();
example.echoProp();
const echoPropRef = example.echoProp;
echoPropRef();

In the console you will see 1 and then undefined. This is because:

  1. Example:echoProp's this reference is instance of Example class, so it see also prop property.
  2. if you pass Example:echoProp's reference to another variable (const echoPropRef = example.echoProp - there is no ()), then this reference is changed into undefined.

bind function "freezes" this reference, so it will be always the same; in you example it will be reference to Toggle class.

The bind is necessary if you use code like

<button onClick={this.handleClick}>
about 4 years ago · Juan Pablo Isaza Relatório

0

When handleClick is triggered by the button, this value inside handleClick will be the click event (this is js behaviour for event listener), but the click event does not have a setState method, which belongs to the component itself. So you want to bind the method to the component, so that this value inside handleClick will be the Toggle component which has the setState method

about 4 years ago · Juan Pablo Isaza Relatório

0

The this is problem is a general problem in JS (not limited to React).

Normally inside a method, this refers to the current object instance.

Inside an event listener (even if its a method), this refers to the event target.

The this in this.setState must refer to the Component instance.

So to change the value of this, binding is done in the constructor.

Another solution would be

onClick={(evt)=>{handleClick(evt)}}

In this case handleClick ceases being an event listener and the actual event listener would be the anonymous function inside onClick.

The simplest alternative is :

handleClick=(evt)=>{
    this.setState(...)
}

Arrow functions do not have their own this & hence acquire the current object (component) instance as this.

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