-1

I have a static method which returns a promise:

static getInfo(ID)
 { return new Promise((resolve, reject) => {   
    api.getList(ID)
      .then((List) => {
        resolve(Info);
      })
      .catch((error) => {
        // here I need to access this.props which is undefined
        console.log(this.props);

      });
     .catch(reject);
   });
 }

this.props is undefined within the promise. how can I access it?

Adel
  • 3,542
  • 8
  • 30
  • 31

1 Answers1

2

As mentioned in a comment on the question, you are using this inside a static method which by definition is not associated with an instance (and hence, this wont be pointing to a component instance).

You can either:

  1. Make it an instance method by removing static
  2. OR, Pass the props/component instance as an argument to the method.

    static getInfo(ID,props){...}

    Component.getInfo(ID, this.props) //In your component where this.props is available

Chirag Ravindra
  • 4,760
  • 1
  • 24
  • 35