将Remburger按钮添加到React Native Navigation

时间:2017-05-16 17:11:54

标签: react-native navigation hamburger-menu

我对React-Native很新,所以我肯定可能会遗漏一些东西。但我想要做的就是在主导航栏的设置页面中添加一个汉堡包类型按钮。我在主要部分设置了一个链接,就像我想要汉堡包按钮一样。 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>

4 个答案:

答案 0 :(得分:13)

有了这个,你就非常接近解决方案。

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

鲜为人知的事实是navigationOptions接受返回导航选项的函数。该函数接受一些道具,navigation其中一个。知道这一点,稍微调整你的代码。

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

答案 1 :(得分:1)

使用相同的问题https://github.com/react-community/react-navigation/issues/1539

检查此链接

检查 navigationOptions

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

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

答案 2 :(得分:0)

在上面的代码中,您似乎在向侧边栏添加选项并导航到侧边栏菜单。

//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" />
        );
      }
    }

通过这种方式,您可以创建尽可能多的抽屉选项..现在如何组合抽屉选项:

// react navigation为您提供了DrawerNavigator API

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

抽屉还附带了一个支柱,它是screenProps = {/ *这个道具将传递给屏幕组件和导航选项作为props.screenProps * /},如下所示:

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

以下是导航器为打开/关闭抽屉提供反应的道具。

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

您也可以根据您设置抽屉样式,如下所示:

drawerWidth - 抽屉的宽度 drawerPosition - 左边或右边的选项。默认是左侧位置。 contentComponent - 默认情况下,抽屉中没有可用的滚动视图。要在抽屉中添加滚动视图,您需要在配置中添加contentComponent。 contentOptions - 顾名思义,这些用于为活动和非活动抽屉项目(标签)提供颜色。

干杯:)

答案 3 :(得分:0)

嘿,您可以签出我的sample code,它是react-navigation的完整抽屉样品。

enter image description here enter image description here

希望有帮助!