简体   繁体   English

React Native-在另一个函数中无法识别该函数

[英]React Native - Function is not recognized inside Another Function

I'm using Firebase Realtime Storage. 我正在使用Firebase实时存储。 I'm trying to call function from inside the firebase ref.on() function. 我正在尝试从firebase ref.on()函数内部调用函数。

My code: 我的代码:

export default class HomeScreen extends React.Component {

  constructor(props) {
    super(props);

    ref.on("value", function (snapshot) {
      dosomething(snapshot.val()) //<-- not recognized
    }, function (errorObject) {
      console.log("The read failed: " + errorObject.code);
    });
   } 

  dosomething(val){
    //...
  }

  //....
});

I'm trying to make the dosomething() function to get called from the ref.on() function but React don't recognize the dosomething() function 我试图让dosomething()函数来获取从所谓ref.on()函数,但阵营不承认dosomething()函数

Why is it happened recognize and How do I fix it? 为什么会发生识别,我该如何解决?

Thanks for your help :) 谢谢你的帮助 :)

you'll need to change function (snapshot) { to (snapshot) => { 您需要将function (snapshot) {更改为(snapshot) => {

now the this inside that callback is the this value of the enclosing lexical context - ie the constructor 现在this是回调里面是this封闭词汇方面的价值-即构造

and then you can use this.dosomething - because dosomething is a property of this 然后你可以使用this.dosomething -因为dosomething是属性this

export default class HomeScreen extends React.Component {

  constructor(props) {
    super(props);

    ref.on("value", (snapshot) => {
      this.dosomething(snapshot.val())
    }, function (errorObject) {
      console.log("The read failed: " + errorObject.code);
    });
   } 

  dosomething(val){
    //...
  }

  //....
});

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

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