Add hamburger button to React Native Navigation

22,998

Solution 1

Having this, you're very close to solution.

static navigationOptions = {
  title: 'Welcome',
  headerLeft: <Button onPress={ WHAT GOES HERE?? } title= "=" />
};

Little know fact is navigationOptions accept function which return navigation options. That function accept some props, navigation one of them. Know this, adjust your code a little.

static navigationOptions = function(props) {
  return {
    title: 'Welcome',
    headerLeft: <Button onPress={() => props.navigation.navigate('DrawerOpen')} title= "=" />
  }
};

Solution 2

check this link with same issue https://github.com/react-community/react-navigation/issues/1539

check navigationOptions

 navigationOptions: ({ navigation }) => ({
              title: 'Schedules',  // Title to appear in status bar
              headerLeft: <Icon name="menu" size={35}
                         onPress={ () => navigation.navigate('DrawerOpen') } />

from

   const SchedulesStack = StackNavigator({
  Schedules: {
    screen: SchedulesScreen,
    navigationOptions: ({ navigation }) => ({
      title: 'Schedules',  // Title to appear in status bar
      headerLeft: <Icon name="menu" size={35} onPress={ () => navigation.navigate('DrawerOpen') } />
    })
  }
});

const Homestack = StackNavigator({
  Home: {
    Screen: Home
    navigationOptions: ({ navigation }) => ({
      title: 'Home',  // Title to appear in status bar
      headerLeft: <Icon name="menu" size={35} onPress={ () => navigation.navigate('DrawerOpen') } />
    })
  }
});

const Root = DrawerNavigator({
  Home: {
    screen: HomeStack,
    navigationOptions: {
      title: 'Home' // Text shown in left menu
    }
  },
  Schedules: {
    screen: SchedulesStack,
    navigationOptions: {
      title: 'Schedules',  // Text shown in left menu
    }
  }
  }
})
Share:
22,998
Admin
Author by

Admin

Updated on August 09, 2022

Comments

  • Admin
    Admin almost 2 years

    I'm very new to React-Native so I definitely may be missing something. But all I want to do is add a hamburger type button to a settings page in the main Navigation bar. I have set up a link in the main part of that works the way I want hamburger button to work. Screenshot

    import React from 'react';
    import { AppRegistry, Text, View, Button } from 'react-native';
    import { StackNavigator } from 'react-navigation';
    
    class HomeScreen extends React.Component {
      static navigationOptions = {
        title: 'Welcome',
        headerLeft: <Button onPress={ WHAT GOES HERE?? } title= "=" />
      };
      render() {
        const { navigate } = this.props.navigation;
        return (
            <Button
              onPress={() => navigate('Settings')}
              title="Link to Settings" />
        );
      }
    }
    
    class Settings extends React.Component {
        static navigationOptions = {
            title: 'Settings',
            headerLeft: <Button title= "=" />
        };
        render() {
            return <Text>Hello, Settings!</Text>;
        }
    }
    
    const SimpleApp = StackNavigator({
        Home: { screen: HomeScreen },
        Settings: { screen: Settings}
    });
    
    AppRegistry.registerComponent('NavPractice', () => SimpleApp);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
  • Sanny Nagveker
    Sanny Nagveker over 6 years
    This solution is good but if you want to get rid of button background use ICON headerLeft: <Icon onPress={() => props.navigation.navigate('DrawerOpen')} name= "menu" style={{marginLeft: 10}} />,