简体   繁体   中英

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

I am not able to access 'this' keyword in my static method in react- native, when I try to access it, it's thrown me error like 'this.setState not a function'.

Here is my code.

static getShiftStatus = () =>{
        //for check shift start or not  
        Usermodal.getShiftStatus((isStatus) =>{
            this.setState({isShiftStart: isStatus}) //error occure here.
            console.log(a.state.isShiftStart)
        }) 
    }

this in the inner function points to something else. You need to capture the this from the outside function.

static getShiftStatus = () =>{

        var that = this;  // capture here

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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