Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

97
Vistas
Is there anyway to get rid of binding `this` to `Component` functions in react?

This is what I do when I want to make this of my class functions bind to the class (component) instance. Is there any easier/more proper way to do this in React?

class App extends Component {

  state = {
    currentSection: 1,
    message :{text:''}
  };

 
  constructor(props) {
    super(props);
    this.prevSection = this.prevSection.bind(this);
    this.nextSection = this.nextSection.bind(this);
    this.mobileChanged = this.mobileChanged.bind(this);
  }
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You could use arrow function instead of class method

With arrow function, there will be no this context so you won't have to bind it

class App extends Component {
  state = {
    currentSection: 1,
    message: { text: '' },
  };

  prevSection = () => {}

  nextSection = () => {}

  mobileChanged = () => {}
}

Live example:

Edit boring-ramanujan-rnrx0

about 4 years ago · Juan Pablo Isaza Denunciar

0

If I recall correctly, if you change:

function nextSection() {...}

to

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

After this change, you can remove this and the bind

Please let me know if your component will remain as functional like it was before. I'm not sure if it this will change the behaviour.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda