React Native Drawer 退出按钮

问题描述 投票:0回答:1

我正在制作一个应用程序,我需要添加一种注销和退出应用程序的方法。 我的结构是这样的:

我的抽屉名称“Home”,看起来像这样:

<Drawer.Navigator  initialRouteName='Inicio'screenOptions={{ headerShown : false }} >
  <Drawer.Screen
    name = "Inicio"
    component={Inicio} 
    options={{
      title: 'Inicio',
      drawerIcon: ({focused, size}) =>(
        <FontAwesome5
          name="home"
          size={size}
          color={focused ? '#0a0a0a' : 'black'}
        />
      ),
    }}/>
  
  <Drawer.Screen name = "Codigo" component={Factor}
    options={{
      title: 'Codigo',
      drawerIcon: ({focused, size}) =>(
        <FontAwesome5
          name="file-alt"
          size={size}
          color={focused ? '#0a0a0a' : 'black'}
        />
      ),
    }}
  />

  <Drawer.Screen name = "Cerrar" component={Cerrar}
    options={{
      title: 'Salir',
      drawerIcon: ({focused, size}) =>(
        <FontAwesome5
          name="sign-out-alt"
          size={size}
          color={focused ? '#0a0a0a' : 'black'}
        />
      ),
    }}
  />
  
</Drawer.Navigator>

它看起来有点乱,但这些只是名称旁边有一个图标的屏幕。

所以我想要的是,当您按下抽屉屏幕“Cerrar”时,它会退出应用程序,如果您再次打开它,它会从头开始运行,而不是从最后一个屏幕开始运行。

所以我尝试使用 BackHandler.exitApp() ,它的作用是最小化应用程序,但如果我重新打开它,它不会从头开始,它从最后一个屏幕开始,在我的例子中是抽屉屏幕“Cerrar” .

react-native react-native-navigation react-native-drawer
1个回答
0
投票

您可以根据用户是否登录动态安装不同的导航组件,因此当他们注销时,他们将被带到(登录,注册等...)堆栈,当他们登录时,他们将被带到(登录,注册等...)堆栈将能够看到您的主屏幕

{isLoggedIn ? <YourDrawerNavigator> : <AuthStackNavigator>}

我认为这种方法是首选,尤其是在 IOS 上,因为他们不希望应用程序能够自杀 IIRC

或者您可以手动将导航器重置为其初始状态(代码取决于您使用的导航库)

或者你可以使用像react-native-exit-app这样的东西https://github.com/wumke/react-native-exit-app

© www.soinside.com 2019 - 2024. All rights reserved.