在React Native中如何在静态方法内部访问'this'关键字?

时间:2018-11-21 08:34:44

标签: javascript android ios reactjs react-native

我无法在react-native的静态方法中访问'this'关键字,当我尝试访问它时,它抛出了类似'this.setState not a function'之类的错误。

这是我的代码。

var parts = sentence.match(/\w+/g);

1 个答案:

答案 0 :(得分:-1)

内部函数中的

this指向其他内容。 您需要从外部函数捕获this

static getShiftStatus = () =>{

        var that = this;  // capture here

        Usermodal.getShiftStatus((isStatus) =>{
            that.setState({isShiftStart: isStatus})  // use it here
            console.log(a.state.isShiftStart)
        }) 
    }