Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 可触摸onPress,反应本机导航不工作_Javascript_React Native_React Navigation_Arrow Functions - Fatal编程技术网

Javascript 可触摸onPress,反应本机导航不工作

Javascript 可触摸onPress,反应本机导航不工作,javascript,react-native,react-navigation,arrow-functions,Javascript,React Native,React Navigation,Arrow Functions,下面的代码可以工作,单击时会显示loginStack堆栈导航器 <TouchableWithoutFeedback onPress={ this.navigateToScreen('loginStack') }> <View><Text>Click Me</Text></View> </TouchableWithoutFeedback> 因为我需要管理多个东西,我把它转换成了一个箭头函数,但它不起

下面的代码可以工作,单击时会显示loginStack堆栈导航器

<TouchableWithoutFeedback 
    onPress={ this.navigateToScreen('loginStack') }>
        <View><Text>Click Me</Text></View>
</TouchableWithoutFeedback>
因为我需要管理多个东西,我把它转换成了一个箭头函数,但它不起作用。按下时,以下代码不起任何作用。完全没有反应

<TouchableWithoutFeedback 
    onPress={ () => this.navigateToScreen('loginStack') }>
        <View><Text>Click Me</Text></View>
</TouchableWithoutFeedback>
this.navigateToScreen('loginStack')}>
点击我
如何修复此问题,以便在onPress函数上运行多行代码,如下所示

<TouchableWithoutFeedback 
    onPress={ () => { this.navigateToScreen('loginStack') 
                      AsyncStorage.removeItem('token') 
                    }}>
        <View><Text>Click Me</Text></View>
</TouchableWithoutFeedback>
{this.navigateToScreen('loginStack'))
AsyncStorage.removeItem('token')
}}>
点击我

无反馈触摸屏始终需要有子视图组件

<TouchableWithoutFeedback onPress={ this.navigateToScreen('loginStack') }}>
  <View>
      <Text>Click Me</Text>
  </View>
</TouchableWithoutFeedback>

点击我

您的
导航屏幕
函数正在返回另一个函数,在您的工作示例中,该函数是按下时调用的函数。如果将onPress更改为arrow函数,则调用的是
navigateToScreen
,而不是它返回的函数

改成

<TouchableWithoutFeedback 
    onPress={ () => {
        this.navigateToScreen('loginStack')()
        AsyncStorage.removeItem('token')
}}>
    <View><Text>Click Me</Text></View>
</TouchableWithoutFeedback>

我建议您使用此选项,但请记住,您当前正在使用的示例在这种情况下将不再适用。

我已编辑了代码,以包含作为子视图的视图。但它似乎仍然不起作用!我认为现在这两个环节将有助于找到解决办法和解决办法
<TouchableWithoutFeedback 
    onPress={ () => {
        this.navigateToScreen('loginStack')()
        AsyncStorage.removeItem('token')
}}>
    <View><Text>Click Me</Text></View>
</TouchableWithoutFeedback>
navigateToScreen = (route) => {
        const navigationAction = NavigationActions.navigate({
            routeName: route
        })
        this.props.navigation.dispatch(navigationAction)
    }