繁体   English   中英

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

[英]How to access 'this' keyword inside of static method in react native?

我无法在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)
        }) 
    }

内部函数中的this指向其他东西。 您需要从外部函数捕获this

static getShiftStatus = () =>{

        var that = this;  // capture here

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM