如何在本机的onpress事件上调用两个函数?

时间:2018-11-13 07:04:02

标签: javascript android reactjs react-native

在下面的代码中,我想在onpress事件上调用这两种方法,但是我遇到了一些问题,请提供一些如何在onpress事件上调用两种方法的想法。

一种方法,用于在事件发生时开始聊天,第二种方法用于更改图像。谢谢

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>

2 个答案:

答案 0 :(得分:0)

要在onPress上调用多个功能,请用分号分隔调用,例如:

onPress{() => { this.functionOne(); this.functionTwo(); }

答案 1 :(得分:0)

合并要在一个函数中调用的两个函数,并命名为“ onPressBlaBlaButton”。然后在render方法中调用此函数。

<View>
  <TouchableOpacity
     activeOpacity={0.5}
     onPress={this.onPressBlaBlaButton}>
    <Image source={this.state.uri} />
  </TouchableOpacity>
</View>