Adding multiple events to onPress function in React Native

拜拜、爱过 提交于 2019-12-07 01:35:29

问题


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

回答1:


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

<Button onPress={this.yourFunction.bind(this)}}>



回答2:


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


来源:https://stackoverflow.com/questions/43198909/adding-multiple-events-to-onpress-function-in-react-native

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!