1

In the below code, I want to call both methods on the onPress event, but I'm facing some issues. How can I call two methods on an onpress event?

One method for start chatting and the second for changing images on the onpress event.

chatStart = () => {
    var { msg } = this.state;
    var { o_code } = this.state;
    var ucod = o_code;
    //console.log(o_code);
    var { session } = this.state;
    //console.log(session);
    var { ocod } = this.state;
    //console.log(ocod);
    var { Name } = this.state;
    var user_name = Name;

    var request = new XMLHttpRequest();
    request.onreadystatechange = e => {
        if (request.readyState !== 4) {
            return;
        }

        if (request.status === 200) {
            console.log("success", request.responseText);
        } else {
            console.warn("error");
        }
    };

    // var msg = "good things take some time";

    console.log(user_name);

    request.open(
        "POST",
        "http://www.aonde.biz/mobile/getChat.php?ocod=" +
        ocod +
        "&ucod=" +
        ucod +
        "&session=" +
        session,
        true
    );
    request.setRequestHeader(
        "Content-type",
        "application/x-www-form-urlencoded"
    );
    request.send("message=" + msg + "&name=" + user_name + "&ocod=" + ocod);
};

changeImage = () => {
    console.log("state changed!");
    this.setState({
      uri: require("./35-reject-red.png")
    });
  };

<View>
  <TouchableOpacity activeOpacity={0.5} onPress={this.changeImage}>
    <Image source={this.state.uri} />
  </TouchableOpacity>
</View>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Atul Tiwari
  • 544
  • 1
  • 7
  • 17

2 Answers2

3

To call multiple functions on onPress, separate the calls with semicolons, like:

onPress={() => { functionOne(); functionTwo(); }}
Machalvan
  • 71
  • 1
  • 7
0

Merge the two functions that you want to call in one function and give it a name like 'onPressBlaBlaButton'. And in the render method, call this function.

<View>
  <TouchableOpacity
     activeOpacity={0.5}
     onPress={this.onPressBlaBlaButton}>
    <Image source={this.state.uri} />
  </TouchableOpacity>
</View>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
VolkanSahin45
  • 1,922
  • 12
  • 25