使用react Native DrawerNavigator注销

时间:2018-01-05 09:52:26

标签: react-native navigation-drawer logout react-navigation

我正在构建一个本机应用程序(App Image),它在Drawer Navigator上有Logout链接。

代码如下

 const DrawerScreen = DrawerNavigator({
    ..........
    logout: {
        screen: Component   
    },
})

export default DrawerScreen;

但问题是,它只是加载组件屏幕。我需要调用一个方法,我可以执行Asyncstorage清除并导航到LoginPage。

8 个答案:

答案 0 :(得分:13)

您可能想要在抽屉中添加一个按钮。 如果是这样,您的代码将如下所示。

const Drawer = DrawerNavigator(
{
    mainpage:{screen:MyScreen}
},
{
    contentComponent:(props) => (
        <View style={{flex:1}}>
            <SafeAreaView forceInset={{ top: 'always', horizontal: 'never' }}>
                <DrawerItems {...props} />
                <Button title="Logout" onPress={DO_SOMETHING_HERE}/>
            </SafeAreaView>
        </View>
    ),
    drawerOpenRoute: 'DrawerOpen',
    drawerCloseRoute: 'DrawerClose',
    drawerToggleRoute: 'DrawerToggle'
})

您应该导入import {DrawerItems} from 'react-navigation';才能使其正常运行。

答案 1 :(得分:3)

const DrawerNavigation = createDrawerNavigator(
  {
    Mainpage: {
      screen: Mainpage
    }
  },
  {
    contentComponent:(props) => (
      <View style={{flex:1}}>
          <SafeAreaView forceInset={{ top: 'always', horizontal: 'never' }}>
            <DrawerItems {...props} />
            <TouchableOpacity onPress={()=>
              Alert.alert(
                'Log out',
                'Do you want to logout?',
                [
                  {text: 'Cancel', onPress: () => {return null}},
                  {text: 'Confirm', onPress: () => {
                    AsyncStorage.clear();
                    props.navigation.navigate('Login')
                  }},
                ],
                { cancelable: false }
              )  
            }>
              <Text style={{margin: 16,fontWeight: 'bold',color: colors.textColor}}>Logout</Text>
            </TouchableOpacity>
          </SafeAreaView>
      </View>
    ),
    drawerOpenRoute: 'DrawerOpen',
    drawerCloseRoute: 'DrawerClose',
    drawerToggleRoute: 'DrawerToggle'
  }
);

答案 2 :(得分:1)

您可以使用Component类执行Asyncstorage清除并导航登录页面。如果你使用react-navigation this.props有导航对象。

class YourComponent extends Component {
    constructor(props) {
        super(props);
     }

    componentWillMount() {
        Asyncstorage.clear();
        this.props.navigation.navigate('LoginPage')
    }
}
export default YourComponent;

答案 3 :(得分:0)

您可以创建一个模式,为您执行此操作。点击退出 - &gt;使用可见属性显示模态并单击是然后关闭模态 - &gt;导航到登录屏幕。

答案 4 :(得分:0)

使用AlertView兄弟会有所帮助,但在上面的示例中,如果你有一个标题而不是这个(this.props.navigation.navigate(&#39; LoginPage&#39;))

不会有任何意义

您的视图将加载到标题

下方

答案 5 :(得分:0)

import {StyleSheet,AsyncStorage,Alert, View,SafeAreaView, Text, ActivityIndicator, Dimensions, TextInput,Image, TouchableOpacity, TouchableHighlight} from 'react-native';
import {DrawerItems,DrawerActions} from 'react-navigation-drawer';

 {
            contentComponent:(props) => (
              <View style={{flex:1}}>
                <SideMenu {...props}/>
                    <DrawerItems {...props} />
                    <TouchableOpacity onPress={()=>
                      Alert.alert(
                        'Log out',
                        'Do you want to logout?',
                        [
                          {text: 'Cancel', onPress: () => {this.props.navigation.dispatch(DrawerActions.closeDrawer()) }},
                          {text: 'Confirm', onPress: () => {
                            AsyncStorage.clear();
                            props.navigation.navigate('LoginScreen')
                          }},
                        ],
                        { cancelable: false }
                      )
                    }>
                      <Text style={{margin: 16,fontWeight: 'bold',color: 'red'}}>Logout</Text>
                    </TouchableOpacity>
              </View>
            ),
            drawerOpenRoute: 'DrawerOpen',
            drawerCloseRoute: 'DrawerClose',
            drawerToggleRoute: 'DrawerToggle'
    },

答案 6 :(得分:0)

如果您要在第5版导航和功能组件中寻找答案,

MapView

如果您想在抽屉中隐藏一个选项,这也将为您提供帮助。

答案 7 :(得分:0)

这是一种使用反应导航库在抽屉中实现注销的方法。

<Drawer.Screen
    name="Logout"
    component={<Any Dummy Component>}
    listeners={({ navigation }) => ({ 
        state: (e) => {
           if (e.data.state.index === 3) {
              // 3 is index of logout item in drawer

              navigation.replace("Login")
           }
        }
    })}
/>