0

Why am I still get this.state of undefined error when a state has a default state declared in the constructor?

constructor() {
    super()
    this.state = {
      data: data,
      q: null
    }
  }

  filterC(o) {
    if (this.state.q) { //wtf??
      return o['Id'].includes(this.state.q)
    }
    return o
  }

Demo: https://codesandbox.io/s/v35r8vyqwl

Jenny Mok
  • 2,744
  • 9
  • 27
  • 58
  • How is `filterC` called? – Felix Kling Dec 15 '17 at 14:23
  • issue is with this binding, check this how to handle this in different ways in react https://stackoverflow.com/questions/47734862/why-reducer-does-not-respond-to-the-action/47740056#47740056 – ram1993 Dec 15 '17 at 15:03

1 Answers1

0

You need to bind this to the function:

constructor(props) {
    super(props)
    this.state = {
      data: data,
      q: null
    }

    this.filterC = this.filterC.bind(this);
  }

  filterC(o) {
    if (this.state.q) {
      return o['Id'].includes(this.state.q)
    }
    return o
  }
Cornel Janssen
  • 681
  • 1
  • 11
  • 33