Add hamburger button to React Native Navigation

元气小坏坏 提交于 2019-11-28 04:49:09

问题


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>

回答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= "=" />
  }
};



回答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
    }
  }
  }
})



回答3:


In the above code it seems you are adding options to the sidebar and navigating to the sidebar menus.

//sidebar menu no.1
    class HomeScreen extends React.Component {
      static navigationOptions = {
        title: 'Welcome',
        headerLeft: <Button onPress={//action taken when option in the menu bar is clicked} title= "//the title of the screen where you will navigate and the sidebar menu lable" />
      };
      render() {
        const { navigate } = this.props.navigation;
        return (
            <Button
              onPress={() => navigate('Settings')}
              title="Link to Settings" />
        );
      }
    }

In this way you can create as many drawer options as you can.. now how to combine drawer options:

//react navigation provides you with DrawerNavigator API

const MyApp = DrawerNavigator({
  Home: {
    screenA: HomeScreen ,
  },
  Settings: {
    screen: MySettingScreens,
  },
});

The drawer also comes with a prop that is screenProps={/* this prop will get passed to the screen components and nav options as props.screenProps */}, like this :

<MyApp
  screenProps={/* this prop will get passed to the screen components and nav options as props.screenProps */}
/>

Following are the props that react navigator provide to open/close drawer.

this.props.navigation.navigate('DrawerOpen'); // open drawer
this.props.navigation.navigate('DrawerClose'); // close drawer

You can also set the drawer style according to you, like this:

drawerWidth - Width of the drawer drawerPosition - Options are left or right. Default is left position. contentComponent - By default there is no scrollview available in the drawer. In order to add scrollview in the drawer you need to add contentComponent in the configuration. contentOptions - As the name suggest these are used to give color to the active and inactive drawer items (label).

Cheers :)




回答4:


Hey you can checkout my sample code which is a complete drawer sample with react-navigation.

Hope it helps!



来源:https://stackoverflow.com/questions/44007686/add-hamburger-button-to-react-native-navigation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!