I am trying to call multiple functions when I click onPress
using TouchableOpacity
For example:
functionOne(){ // do something } functionTwo(){ // do someting } <TouchableHighlight onPress{() => this.functionOne()}/>
What if I want to call two functions when onPress
is clicked? Is there a way I could call multiple functions?
To call multiple functions when onPress is clicked with React Native, we can assign onPress to a function that calls all the functions. to define the onPress function that calls foo , bar , and baz . Then we set the onPress prop to the onPress function.
Definition of on press : being printed The book is on press now and due out soon.
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()}/>
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(); } } />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With