onPress功能不起作用

时间:2018-05-28 00:57:22

标签: react-native react-navigation

我想在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 个答案:

答案 0 :(得分:1)

应该是:

onPress={() => this.onButtonPress()}

或:

onPress={this.onButtonPress.bind(this)} // bind in constructor for better performance

或:

onButtonPress = () => {
...
onPress={this.onButtonPress}

此外,您的navigateToScreen还有额外的参数:

navigateToScreen = (route, options) => {