【发布时间】:2018-11-21 08:34:44
【问题描述】:
我无法在 react-native 的静态方法中访问“this”关键字,当我尝试访问它时,它会抛出类似“this.setState not a function”这样的错误。
这是我的代码。
static getShiftStatus = () =>{
//for check shift start or not
Usermodal.getShiftStatus((isStatus) =>{
this.setState({isShiftStart: isStatus}) //error occure here.
console.log(a.state.isShiftStart)
})
}
【问题讨论】:
-
静态方法不会在类的实例上调用,因此如果您只删除
static关键字,它应该可以按预期工作。 -
在构造函数中将方法绑定到实例?
标签: javascript android ios reactjs react-native