Passing a value from one function to another function in React.js

dns

I have 2 functions getLocation() and getLocationName().

The getLocation() function executes an XMLHttpRequest and I wish to pass the response to the getLocationName() function in order to display it in a list.

getLocationName = (location) => {
    var locName = location;
    console.log("location name", locName)
    return locName;
}

getLocation = (lat, lon) => {
    var text;
    var xmlhttp = new XMLHttpRequest();
    var url = "https://nominatim.openstreetmap.org/search?format=jsonv2&accept-language=sr-Latn&limit=3&q=" + lat + "," + lon;
    console.log("url: ", url)
    xmlhttp.onreadystatechange = function()
    {
        if (this.readyState == 4 && this.status == 200)
        {
            var resp = JSON.parse(this.responseText);                
            text = resp[0].display_name;
            console.log("response: ", text);
            this.getLocationName(text);
        }
    };
    xmlhttp.open("GET", url, true);
    xmlhttp.send();
}

I can see the response in the console.log() from getLocation, but I get

Uncaught TypeError: this.getLocationName is not a function

I tried calling it without "this." tried to bind it in the constructor like this

constructor(props){
    super(props);
    this.getLocationName = this.getLocationName.bind(this);
}

Also tried calling it this way:

() => this.getLocationName(text); 

No luck unfortunately...

Adam Cai

Yes. This is about 'this'. "xmlhttp" object has its own 'this'. Try this:

getLocation = (lat, lon) => {
    var that = this; //save this
    var text;
    var xmlhttp = new XMLHttpRequest();
    var url = "https://nominatim.openstreetmap.org/search?format=jsonv2&accept-language=sr-Latn&limit=3&q=" + lat + "," + lon;
    console.log("url: ", url)
    xmlhttp.onreadystatechange = function()
    {
        if (this.readyState == 4 && this.status == 200)
        {
            var resp = JSON.parse(this.responseText);                
            text = resp[0].display_name;
            console.log("response: ", text);
            that.getLocationName(text);
        }
    };
    xmlhttp.open("GET", url, true);
    xmlhttp.send();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Passing function in React.js from one function component to another

From Dev

JS : why isn't this value passing from one function to another?

From Javascript

Passing data from one function to another in React

From Dev

Passing variable value from one function to another

From Dev

Passing Results from one React JS Function To Another React JS Function

From Dev

Passing value from one function to another function in C

From Dev

Passing a Variable from one JS function to another function

From Dev

Passing values from one function to another function

From Dev

Passing a variable from one function to another function

From Dev

Passing values from one function to another with React navigation 5

From Dev

Passing string parameters into function from one component to another in react

From Dev

Passing value from one function to another C++

From Dev

Passing variable value from one function to another in autocomplete with AJAX call

From Dev

Passing returned value from one function to another with Python

From Dev

Polymer JS passing function from one component to another?

From Dev

ANGULAR.js passing function from one component to another

From Dev

How to assign a value of a variable from a function in another function in React JS

From Dev

Trouble passing links from one function to another

From Dev

Passing a range from one VB function to another

From Dev

Passing lists from one function to another in Swift

From Dev

React JS - passing multiple functions to another function

From Dev

Passing values from a function to another in JS

From Dev

Passing info from a Form into a function React js

From Dev

Passing two values from one Matlab function to another in one line

From Dev

Data Getting Corrupted while passing from one function to another in node.js

From Dev

Calling one function in another and passing it id, react + redux

From Dev

Fire function from one react component in another

From Dev

Get value from One function and pass it to another

From Dev

Pass value from one function to another to calculate with it

Related Related

  1. 1

    Passing function in React.js from one function component to another

  2. 2

    JS : why isn't this value passing from one function to another?

  3. 3

    Passing data from one function to another in React

  4. 4

    Passing variable value from one function to another

  5. 5

    Passing Results from one React JS Function To Another React JS Function

  6. 6

    Passing value from one function to another function in C

  7. 7

    Passing a Variable from one JS function to another function

  8. 8

    Passing values from one function to another function

  9. 9

    Passing a variable from one function to another function

  10. 10

    Passing values from one function to another with React navigation 5

  11. 11

    Passing string parameters into function from one component to another in react

  12. 12

    Passing value from one function to another C++

  13. 13

    Passing variable value from one function to another in autocomplete with AJAX call

  14. 14

    Passing returned value from one function to another with Python

  15. 15

    Polymer JS passing function from one component to another?

  16. 16

    ANGULAR.js passing function from one component to another

  17. 17

    How to assign a value of a variable from a function in another function in React JS

  18. 18

    Trouble passing links from one function to another

  19. 19

    Passing a range from one VB function to another

  20. 20

    Passing lists from one function to another in Swift

  21. 21

    React JS - passing multiple functions to another function

  22. 22

    Passing values from a function to another in JS

  23. 23

    Passing info from a Form into a function React js

  24. 24

    Passing two values from one Matlab function to another in one line

  25. 25

    Data Getting Corrupted while passing from one function to another in node.js

  26. 26

    Calling one function in another and passing it id, react + redux

  27. 27

    Fire function from one react component in another

  28. 28

    Get value from One function and pass it to another

  29. 29

    Pass value from one function to another to calculate with it

HotTag

Archive