Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react native how to call multiple functions when onPress is clicked

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?

like image 508
kirimi Avatar asked Sep 10 '18 01:09

kirimi


People also ask

How do you call multiple functions onPress in react native?

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.

What is onPress?

Definition of on press : being printed The book is on press now and due out soon.


1 Answers

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(); }  } />          
like image 170
Dacre Denny Avatar answered Sep 25 '22 04:09

Dacre Denny