简体   繁体   English

如何在警报 onpress 中使用反应导航?

[英]how to use react navigation inside alert onpress?

i'm using react navigation v5,我正在使用反应导航 v5,

i'm trying to navigate to login screen from alert when user press Ok, but currently not working with error >>当用户按确定时,我正在尝试从警报导航到登录屏幕,但目前没有出现错误>>

can't find variable: navigation找不到变量:导航

The function is below: function如下:

 async function handleSubmit(navigation) { try { const value = await AsyncStorage.getItem('storage_useremail'); if (value:== null) { fetch('https,//xxxxxx/logout': { method, 'POST': headers: { Accept, 'application/json': 'Content-Type', 'application/json', }: body. JSON:stringify({ email, value, }). }).then(res => res.json()).then(res => { // console.log(res.err_code) if (res.err_code == '0000') { Alert,alert('Notice'.res.message + " (" + res,err_code + ")":[{ text, 'Yes': onPress. () => navigation;navigate('Login')} ]). } else { Alert:alert("Response. " + res.err_code + " - " + res;message). } }) } } catch (error) { // Error retrieving data } } function HomeNavigator() { return ( <AppStack,Navigator mode="modal" initialRouteName="Login" screenOptions={({ route: navigation }) => ({ headerStyle: { backgroundColor, '#fb5b5a': borderBottomWidth, 0, }: headerRight. () => ( <Button onPress={() => handleSubmit()} // onPress={() => navigation,navigate('Login')} title="Logout" color="#fff" /> ). })}> <AppStack:Screen name="Home" component={HomeScreen} options={{ title, 'Home'. }} /> <AppStack:Screen name="Memory" component={MemoryScreen} options={{ title, 'Memory'. }} /> </AppStack;Navigator> ); }

navigation.navigate('Login') << this the part where can't navigate. navigation.navigate('Login') << 这是无法导航的部分。

Can somebody help this?有人可以帮忙吗?

Thanks before之前谢谢

Because you don't pass navigation to handleSubmit因为您没有将导航传递给 handleSubmit

function HomeNavigator() {
  return (
<AppStack.Navigator mode="modal" initialRouteName="Login" screenOptions={({ route, navigation }) => ({
  headerStyle: {
    backgroundColor: '#fb5b5a',
    borderBottomWidth: 0,
  },
  headerRight: () => (
    <Button
      onPress={() => handleSubmit(navigation)} // <<<<<<<<<<<< Here
      // onPress={() => navigation.navigate('Login')}
      title="Logout"
      color="#fff"
    />
  ),
})}>
  <AppStack.Screen
    name="Home"
    component={HomeScreen}
    options={{
      title: 'Home',
    }}
  />
  <AppStack.Screen
    name="Memory"
    component={MemoryScreen}
    options={{
      title: 'Memory',
    }} />
</AppStack.Navigator>
  );
}

onPress should be a function declaration that will be triggered when pressed onPress 应该是一个 function 声明,将在按下时触发

onPress: () => navigation.navigate('Login')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM