Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!