ReactNative踩坑日志——函数绑定this

    ES6语法定义的函数没有自动绑定this,所以在函数中使用了 this.xxx 就会报错,因为没有把类实例等this传进函数中。

    有两种方法绑定this:

    法1:在构造函数中为其他函数绑定this

construtor(props){
   super(props);
   ......
   this.funcName = this.funcName.bind(this)//2:为函数绑定this
}
//1:自定义函数
funcName(args){

}

    法2:用箭头函数自定义函数,自动绑定this

funcName= (args)=>{ //定义箭头函数,赋值给函数名,自动绑定this

}
原文地址:https://www.cnblogs.com/ygj0930/p/7488064.html