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

140
Vistas
Undefined 'this' when handing class method to React component

I have API methods for my React app within an API class:

class API { 

  constructor() {
    //setup axios
  }

  async deleteEquipment(item) {
      return this.axios.delete(...paths and stuff..)
  }
}

export default API(config)

This works fine for most of my code, except when I need to hand it to components through the props, like so:

import API from '../path/to/api.js'

.. code

<PageTable
  deleteItem={API.deleteEquipment}
  ...more props
/>

This doesn't work as it seems this used in the class to reference axios is now undefined.

this.axios leads to Cannot read properties of undefined (reading 'axios')

I have tried late binding, to bind the method to the class like so:

<PageTable
  ...more props
  deleteItem={API.deleteEquipment.bind(API)}
/>

but it doesn't seem to have much effect. Any ideas how to keep the this reference to the class when the method is called by PageTable ?

Thanks in advance!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

try to bind deleteEquipment in constructor

this.deleteEquipment = this.deleteEquipment(this);

also use await with async functions

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