add icon to drawer react-navigation v5
Asked Answered
Z

4

21

I'm trying to add an icon to each of the screens in my react-navigation drawer, but the icon doesn't appear.

Here is my code :

function Drawer() {
  return (
      <Drawer.Navigator 
       drawerStyle={styles.drawer}
        initialRouteName="Home" 
        drawerPosition='right'
        drawerContentOptions={{
        activeTintColor: 'white',
        inactiveTintColor: 'white',
        itemStyle: { alignItems:'flex-end' },
       }}>
        <Drawer.Screen name="AppTab" component={AppTab1} options={{ headerStyleInterpolator: forFade ,title:"home" ,icon:<Image source={require('./images/icons/plumbing-b.png')} style={styles.drawerActive}/> }} />
        <Drawer.Screen name="News" component={NotificationsScreen} options={{ headerStyleInterpolator: forFade ,title:"new items" icon:<Image source={require('./images/icons/plumbing-b.png')} style={styles.drawerActive}/> }} />
        
      </Drawer.Navigator>
    
  );
}


export function App() {
  return (
    <NavigationContainer>
      <Stack.Navigator>
        <Stack.Screen
          options={{
            headerTitleAlign:"center",
            headerRight: ({}) => <HeaderRight />,
            headerLeft: ({}) => <Search />
          }}
          component={Drawer}
          name="Drawer"
        />
        <Stack.Screen name="Product" component={Product} options={{title:"product"}} />
        {/*
         * Rest Screens
         */}
      </Stack.Navigator>
    </NavigationContainer>
  );
}

in documentation, adding icon is only mentioned in DrawerItem:

https://reactnavigation.org/docs/en/drawer-navigator.html

Zoogeography answered 24/2, 2020 at 11:50 Comment(1)
pass custom componentRingworm
D
40

You have to simply add drawerIcon in the options

<Drawer.Navigator>
    <Drawer.Screen name="Home" component={Home}
        options={{
           title: 'Home',
           drawerIcon: ({focused, size}) => (
              <Ionicons
                 name="md-home"
                 size={size}
                 color={focused ? '#7cc' : '#ccc'}
              />
           ),
        }}
   />
</Drawer.Navigator>

you can also pass color directly like this

...
drawerIcon: ({color, size}) => (
            <Ionicons
               name="md-home" size={size} color={color}
            />
          ),
...

here I have used Ionicons for icon, you can use your own icon component or import Ionicons from 'react-native-vector-icons/Ionicons'.

Dendrology answered 19/5, 2020 at 19:14 Comment(0)
E
13

Pass drawerIcon in your screen's options:

options={{
  title: 'Product',
  drawerIcon: ({ focused, size }) => (
    <Image
      source={require('./images/icons/plumbing-b.png')}
      style={[focused ? styles.drawerActive : styles.drawerInActive, { height: size, width: size }]}
    />
}}

https://reactnavigation.org/docs/en/drawer-navigator.html#drawericon

Ester answered 25/2, 2020 at 0:26 Comment(0)
S
2

You can add icons to DrawerItem in DrawerContent component.

in App:

function Drawer() {
  return (
      <Drawer.Navigator 
       drawerStyle={styles.drawer}
        initialRouteName="Home" 
        drawerPosition='right'
        drawerContentOptions={{
          activeTintColor: 'white',
          inactiveTintColor: 'white',
          itemStyle: { alignItems:'flex-end' },
        }}
        drawerContent={props => <DrawerContent {...props}/>}
      >
        <Drawer.Screen name="AppTab" component={AppTab1} options={{ headerStyleInterpolator: forFade ,title:"home" ,style={styles.drawerActive}/> }} />
        <Drawer.Screen name="News" component={NotificationsScreen} options={{ headerStyleInterpolator: forFade ,title:"new items" style={styles.drawerActive}/> }} />

      </Drawer.Navigator>

  );
}

in DrawerContent:

                  <DrawerItem
                    label='News'
                    onPress={() => 
                      props.navigation.navigate('News')}                                           
                    icon={() =>
                      <Image
                        style={styles.icon}
                        source={require('./images/icons/plumbing-b.png')
                      />
                    }
                  />
Supernova answered 25/2, 2021 at 9:2 Comment(0)
H
-2

i configure the Drawer Content using the drawer content: the steps are as follows... 1.Create a drawer with the screens function DrawerStack({ route, navigation }) { return (

  drawerContent={(props) => <DrawerContent {...props} />}

  drawerStyle={{
    backgroundColor: "green",
    alignItems: "center",

    paddingTop: 100
  }}
>
  {/* //it is must to define the screens here */}
  <Drawer.Screen name="Drawer1" component={Drawer1}
  />
  <Drawer.Screen name="Drawer2" component={Drawer2} />
</Drawer.Navigator>

) }

2.Customise the Drawer Content Using drawerContent:

function DrawerStack({ route, navigation }) { return (

  drawerContent={(props) => <DrawerContent {...props} />}

  drawerStyle={{
    backgroundColor: "green",
    alignItems: "center",

    paddingTop: 100
  }}
>
  {/* //it is must to define the screens here */}
  <Drawer.Screen name="Drawer1" component={Drawer1}
  />
  <Drawer.Screen name="Drawer2" component={Drawer2} />
</Drawer.Navigator>

) }

Horary answered 25/2, 2020 at 8:22 Comment(1)
Brother you have pasted same code in both the places.Windom

© 2022 - 2024 — McMap. All rights reserved.