Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react Navigation 3.x open drawer from header button?

I want to create a header on top with title for each screen and button on the right to open the drawer in react navigation 3.x

In the code below the header does not show.

//Updated with Current code

  import React, { Component } from 'react';
import { Button } from 'react-native';
import {
  createStackNavigator,
  createDrawerNavigator,
  createAppContainer
} from 'react-navigation';

import MyHomeScreen from './components/HomeScreen';
import MyNotificationsScreen from './components/ProfileScreen';

const MyDrawerNavigator = createDrawerNavigator(
  {
    Home: {
      screen: MyHomeScreen
    },
    Notifications: {
      screen: MyNotificationsScreen
    }
  },
  {
    initialRouteName: 'Home',
    navigationOptions: navigationOptionsHeader
  }
);

const navigationOptionsHeader = ({ navigation }) => {
  return {
    headerTitle: 'MY Home',
    headerRight: (
      <Button
        onPress={() => navigation.toggleDrawer()}
        title="Info"
        color="#222"
      />
    )
  };
};

const AppContainer = createAppContainer(MyDrawerNavigator);

class App extends Component {
  render() {
    return <AppContainer />;
  }
}
export default App;
like image 488
velvetInk Avatar asked Dec 08 '18 06:12

velvetInk


Video Answer


2 Answers

Use this inside your screen class

 static navigationOptions = ({ navigation }) => {
        return {
        title: 'Home',
        headerLeft: (
          < Icon name="menu" size={30} style={{marginStart:10}} backgroundColor="#000000" onPress={() => navigation.openDrawer()} > < /Icon>
        ),
      };
    };
like image 62
Shivam Raj Avatar answered Sep 17 '22 20:09

Shivam Raj


try this

const MyDrawerNavigator = createDrawerNavigator(
      {
        Home: {
          screen: MyHomeScreen
        },
        Notifications: {
          screen: MyNotificationsScreen
        }
      },
      {
        initialRouteName: 'Home'
     navigationOptions: navigationOptionsHeader,

      }
    );


    const navigationOptionsHeader=({navigation})=>{


        return {

      headerRight: (
          <Button
            onPress={() => navigation.toggleDrawer();
    }
            title="Info"
            color="#222"
          />
        )
      };

    }

you can also add other stuffs in header like this

    const navigationOptionsHeader=({navigation})=>{


        return {

      headerRight: (
          <Button
            onPress={() => navigation.toggleDrawer();
    }
            title="Info"
            color="#222"
          />
        )

  headerLeft : <headerLeft/>,
  title: //Header Title
  headerStyle: { backgroundColor: '#161616', height:48, },
  headerTitleStyle:{ color:'#cd9bf0', fontWeight: '400', alignSe
      };

    }
like image 30
vignesh Avatar answered Sep 16 '22 20:09

vignesh