简体   繁体   English

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

[英]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'. 我无法在React Native的静态方法中访问'this'关键字,当我尝试访问它时,它抛出了类似'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. 内部函数中的this指向其他东西。 You need to capture the this from the outside function. 您需要从外部函数捕获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.

相关问题 React Native:在静态函数内访问组件状态 - React Native : Access Component state inside a static function 如何在 static 方法中访问 object 值? - How to access object value inside static method? 通过“this”关键字访问静态方法中的类属性 - Access a Class property within the static method by "this" keyword 如何在 class 的方法内的事件侦听器中使用“this”关键字,而不会失去对 class arguments 和方法 ZDBC11CAA5BDA99F77E6FBDABDBDBDA99F77E6FBDABDBD77 的访问权限 - How to use “this” keyword inside event listener inside a method of a class without losing access to class arguments and method arguments? 如何在 React Native 的另一个函数中访问 c​​onst? - How to access const inside another function in React Native? 我如何在本机反应中访问组件的内部属性(标题) - how can i access the inside property (title) of an component in react native 何时在React类组件中的方法之前使用static关键字 - When to use the static keyword before a method in a React class component 如何在React Native中从NavigationBar访问js文件的函数或方法? - How to access a function or method of js file from navigationBar in React Native? React Native,意外的关键字“This” - React Native, Unexpected Keyword “This” 如何在不使用 this 关键字的情况下访问 Javascript 中同一类的方法内的类的构造函数中定义的属性? - How can I access a property defined in a constructor of a class inside a method of the same class in Javascript without using this keyword?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM