当状态在构造函数中声明了默认状态时,为什么我仍然得到this.state的未定义错误?
constructor() {
super()
this.state = {
data: data,
q: null
}
}
filterC(o) {
if (this.state.q) { //wtf??
return o['Id'].includes(this.state.q)
}
return o
}
答案 0 :(得分:0)
您需要将此绑定到函数:
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
}