react native arrow functions and geoQueries

不问归期 提交于 2019-12-11 14:54:55

问题


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

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)");
            }
        }    
    })
    }

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?


回答1:


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) => 

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



来源:https://stackoverflow.com/questions/50218267/react-native-arrow-functions-and-geoqueries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!