Adding multiple events to onPress function in Reac

2019-04-25 10:54发布

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})}>

2条回答
时光不老,我们不散
2楼-- · 2019-04-25 11:26

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})
}}>
查看更多
太酷不给撩
3楼-- · 2019-04-25 11:44
yourFunction(){
 Actions.ideaOnClick();
 this.setState({views: ++this.state.views});
}

<Button onPress={this.yourFunction.bind(this)}}>
查看更多
登录 后发表回答