Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding multiple events to onPress function in React Native

When I press the button, it supposed to navigate to another page and it also increase the view count.

But I can't figure out anyway to implement them at the same time.

This is my code:

<Button onPress={Actions.ideaOnClick}>
<Button onPress={() => this.setState({views: ++this.state.views})}>
like image 666
Avikrit Khati Avatar asked Apr 04 '17 05:04

Avikrit Khati


People also ask

How do I add multiple functions to onPress 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.

How do you call two functions at a time in React native?

Call Multiple Functions onClick in Reactlog('hello!' ); } function waveHello() { console. log('wave'); } return ( <div> <button onClick={() => { greeting(); waveHello(); }}> I'm a button </button> </div> ); } export default App; The example above may look similar to the first solution, but it's very different.


2 Answers

Just create a function that does both things. No other way around it (if a lib has a utillity for that it would do something similar) since onPress is a single function.

<Button onPress={() => {
    Actions.ideaOnClick()
    this.setState({views: ++this.state.views})
}}>
like image 141
Moti Azu Avatar answered Jan 20 '23 04:01

Moti Azu


yourFunction(){
 Actions.ideaOnClick();
 this.setState({views: ++this.state.views});
}

<Button onPress={this.yourFunction.bind(this)}}>
like image 42
Gabriel Mesquita Avatar answered Jan 20 '23 04:01

Gabriel Mesquita