0

When using this on react class component like this:

someFunc() {
  console.log(this.props.someProp);
}

I get undefined.

But when I use this inside a lifecycle method of react like this:

componentDidMount() {
  console.log(this.props.someProp);
}

I get the value of someProp.

I'm searching for an explanation for this behavior, can someone help?

qwerty
  • 45
  • 4
  • Are you using someFunc as a _callback_? Give a [mre]. And what do you mean "vs arrow function" - what arrow function? – jonrsharpe Mar 25 '22 at 13:02
  • 1
    you have to bind the `this` in the function you define yourself. eg in your costructor you have to type `this.someFunc = this.someFunc.bind(this)` – coglialoro Mar 25 '22 at 13:05
  • You'll need to show us how you use (call) `someFunc`, and how you want to use an arrow function instead – Bergi Mar 25 '22 at 13:06

0 Answers0