本文介绍了反应原生如何在点击 onPress 时调用多个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 TouchableOpacity

例如:

functionOne(){
// do something
}

functionTwo(){
// do someting
}

<TouchableHighlight onPress{() => this.functionOne()}/>

如果我想在点击 onPress 时调用两个函数怎么办?有没有办法可以调用多个函数?

What if I want to call two functions when onPress is clicked? Is there a way I could call multiple functions?

推荐答案

有几种方法可以实现这一点.一种选择是定义一个调用 functionOnefunctionTwo 的函数,并将其传递给您的 onPress 处理程序,如下所示:

There are a few ways to achieve this. One option would be to define a function that calls functionOne and functionTwo, and pass that on your onPress handler like so:


functionOne(){
// do something
}

functionTwo(){
// do something
}

functionCombined() {
    this.functionOne();
    this.functionTwo();
}

<TouchableHighlight onPress={() => this.functionCombined()}/>

或者,更简洁地,您可以像这样在 JSX 中内联表达 functionCombined:

Alternatively, and more concisely, you could express functionCombined inline in your JSX like so:


functionOne(){
// do something
}

functionTwo(){
// do someting
}

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


这篇关于反应原生如何在点击 onPress 时调用多个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 16:29