[英]Touchable onPress with React Native Navigation not working
The below code works and when clicked takes me the loginStack Stack Navigator 下面的代码有效,当单击时,我将使用loginStack Stack Navigator
<TouchableWithoutFeedback
onPress={ this.navigateToScreen('loginStack') }>
<View><Text>Click Me</Text></View>
</TouchableWithoutFeedback>
My NavigateToScreen function looks like this 我的NavigateToScreen函数看起来像这样
navigateToScreen = (route) => () => {
const navigationAction = NavigationActions.navigate({
routeName: route
})
this.props.navigation.dispatch(navigationAction)
}
Since I need to manage multiple stuff, I converted it to an arrow function and it doesn't work. 由于我需要管理多个内容,因此将其转换为箭头功能,但它不起作用。 The below code does nothing when pressed.
按下时,以下代码不执行任何操作。 No response at all.
完全没有回应。
<TouchableWithoutFeedback
onPress={ () => this.navigateToScreen('loginStack') }>
<View><Text>Click Me</Text></View>
</TouchableWithoutFeedback>
How do I fix this so I can run multiple lines of code on the onPress function like given below? 如何解决此问题,以便可以在onPress函数上运行多行代码,如下所示?
<TouchableWithoutFeedback
onPress={ () => { this.navigateToScreen('loginStack')
AsyncStorage.removeItem('token')
}}>
<View><Text>Click Me</Text></View>
</TouchableWithoutFeedback>
Your navigateToScreen
function is returning another function, which in your working example is the function being called when pressed. 您的
navigateToScreen
函数正在返回另一个函数,在您的工作示例中是按下该函数时所调用的函数。 Where you changed onPress to an arrow function, you are calling navigateToScreen
but not the function it returns. 在将onPress更改为箭头功能的地方,您正在调用
navigateToScreen
但没有调用它返回的功能。
Changing it to 更改为
<TouchableWithoutFeedback
onPress={ () => {
this.navigateToScreen('loginStack')()
AsyncStorage.removeItem('token')
}}>
<View><Text>Click Me</Text></View>
</TouchableWithoutFeedback>
should work, or you could remove the second arrow function in your navigateToScreen
function so it no longer returns a function but executes the code itself 应该工作,或者你可以删除第二个箭头功能在您的
navigateToScreen
功能,使其不再返回功能,但执行的代码本身
navigateToScreen = (route) => {
const navigationAction = NavigationActions.navigate({
routeName: route
})
this.props.navigation.dispatch(navigationAction)
}
I would recommend this option, but keep in mind your currently working example would no longer work in this case. 我建议使用此选项,但是请记住,在这种情况下,您当前正在使用的示例将不再起作用。
TouchableWithoutFeedback always needs to have child View component. TouchableWithoutFeedback始终需要具有子View组件。
<TouchableWithoutFeedback onPress={ this.navigateToScreen('loginStack') }}>
<View>
<Text>Click Me</Text>
</View>
</TouchableWithoutFeedback>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.