onPress 函数不起作用
2018-05-28
201
我想在 onPress 上添加多个函数。所以我有这样的 onPress 函数。但是它不起作用。
navigateToScreen = (route) => () => {
const navigateAction = NavigationActions.navigate({
routeName: route
});
this.props.navigation.dispatch(navigateAction);
}
onButtonPress() {
this.navigateToScreen('Home', { isStatusBarHidden: false })
}
render() {
return (
<View style={styles.container}>
<TouchableOpacity
style={styles.menuItem}
onPress={() => {this.onButtonPress.bind(this)}
>
...
navigateToScreen
不起作用,请告诉我原因。
1个回答
它应该是:
onPress={() => this.onButtonPress()}
或:
onPress={this.onButtonPress.bind(this)} // bind in constructor for better performance
或:
onButtonPress = () => {
...
onPress={this.onButtonPress}
此外,您的
navigateToScreen
有一个额外的参数级别:
navigateToScreen = (route, options) => {
Roy Wang
2018-05-28