反应本地箭头功能和geoQueries [英] react native arrow functions and geoQueries

查看:89
本文介绍了反应本地箭头功能和geoQueries的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还在为箭头功能苦苦挣扎.我以前发布过这些帖子:

I am still struggling a little bit with arrow functions. I have previously made these posts:

  • react native and globally accessible objects
  • react native arrow functions and if statements

但是现在我在如何将它们与geoQuery结合使用上遇到了一个新问题.我尝试了如下所示的代码:

But now I am having a new issue with how these can be used with geoQuery. I have tried the code shown below:

    myFunction = () => {

    var onKeyMovedRegistration = () => geoQuery.on("key_moved", function(key, location, distance) {
        console.log('Test log');
        if (key != this.props.userID) {
            arraySearchResult = findKeyInArray(key, this.props.arrayOfKeys);
            if (arraySearchResult != "keyNotFound") {    
                console.log(key + " moved within query to " + location + " (" + distance + " km from center)");
            }
        }    
    })
    }

因此,绑定了myFunction,并且我也尝试绑定geoQuery,否则,如果我使用原始行,则无法访问this.props.userID和this.props.arrayOfKeys:

So myFunction is bound, and I am trying to bind the geoQuery as well, as otherwise this.props.userID and this.props.arrayOfKeys are not accessible if I use the original line:

var onKeyMovedRegistration = geoQuery.on("key_moved", function(key, location, distance) {

通过上述尝试,没有任何反应.我没有获得测试日志"到控制台.没有崩溃或错误,很明显,这不是应该怎么做的.

With the attempt above, nothing fires. I do not get "Test log" to the console. There is no crash or error, it's just clearly not how this is supposed to be done.

任何人都知道我该如何克服吗?

Anyone know how I can get past this one?

推荐答案

由于您的geoFire listener函数未绑定 ,因此您无法访问它的类级支持.

Since your geoFire listener function is not bound therefore you can't access class level props to it.

因此,最简单的方法是将箭头功能用作

Therefore the simplest way would be to use the arrow function here as

geoQuery.on("key_moved", (key, location, distance) => 

此后,this将绑定到该类,您将可以访问该类级别的道具.

After this, this will be bound to the class and you will be able to access the class level props.

这篇关于反应本地箭头功能和geoQueries的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆