Search code examples
react-nativenavigation-drawerreact-navigation

How to add extra item at the bottom of the drawer navigation manually (like logout button)?


I want to add logout button to the bottom of the drawer navigation in my RN app.

As you can see the Logout button is located at the bottom of the drawer menu. How can I move it to the bottom of the drawer panel?

I am trying to use contentComponent the following way:

const DrawerWithLogoutButton = (props) => (
  <ScrollView>
    <SafeAreaView style={styles.container} forceInset={{ top: 'always', horizontal: 'never' }}>
      <DrawerItems {...props} />
    </SafeAreaView>
    <Button
      style={styles.logoutButton}
      title="Logout"
      onPress={() => props.navigation.navigate('Login') }/>
  </ScrollView>
);

export default Home = createDrawerNavigator({
  // screens
}, {
  // other settings
  contentComponent: DrawerWithLogoutButton,
});

const styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: 'column',
  },
  logoutButton: {
    backgroundColor: 'red',
    position: 'absolute',
    bottom: 0
  }
});

In the result I have the Logout button at the bottom of the menu. But I want it to be located at the bottom of the drawer panel instead

Also I would want the Logout button to look like other menu items and had an icon

Is there a way to create drawer navigator with a menu item which has no screen but is just an action like Logout as in my case?


Solution

  • I was able to align Logout at the bottom of the drawer menu with adding justifyContent: 'space-between' to the ScrollView container. You can see the result in the picture

    the Logout button is located at the bottom of the drawer menu

    The result source code looks the following way:

    const DrawerWithLogoutButton = (props) => (
      <ScrollView contentContainerStyle={{flex: 1,  flexDirection: 'column', justifyContent: 'space-between' }}>
        <SafeAreaView forceInset={{ top: 'always', horizontal: 'never' }}>
          <DrawerItems {...props} />
        </SafeAreaView>
        <TouchableOpacity>
          <View style={styles.item}>
            <View style={styles.iconContainer}>
              <Image source={require('./img/logout.png')} style={styles.icon}></Image>
            </View>
            <Text style={styles.label}>Logout</Text>
          </View>
        </TouchableOpacity>
      </ScrollView>
    );
    
    const styles = StyleSheet.create({
      item: {
        flexDirection: 'row',
        alignItems: 'center',
      },
      label: {
        margin: 16,
        fontWeight: 'bold',
        color: 'rgba(0, 0, 0, .87)',
      },
      iconContainer: {
        marginHorizontal: 16,
        width: 24,
        alignItems: 'center',
      },
      icon: {
        width: 24,
        height: 24,
      }
    });