The hamburger navigation icon in the header of all top-level headings (using a drawer)?

I can set the header options "universally" for some things, for example, make the title color white:

// example screens
const SettingsScreen = () => <View><Text>SettingsScreen...</Text></View>
const List = () => <View><Text>List...</Text></View>
const Item = () => <View><Text>Item...</Text></View>

// create an object to pass on to relevant screens
const navigationOptions = {
      header: {
        style: {
              backgroundColor: '#fff'
            }
        },
    }

// MAIN SCREEN : a screen showing a list with ability to click on an list item and go to a detail page
// ============================

const ListScreens = StackNavigator({
  List: { screen: List, navigationOptions: navigationOptions }, //show a hamburger menu
   Item: { screen: Item, navigationOptions: navigationOptions }, // this is a detail page, so don not show a hamburger menu, rather show a back button
});

const SettingsContainer = StackNavigator({
  Settings: { screen: SettingsScreen },
});


// LOGGED IN DRAWER VIEW : top-level component is a drawer with two menu items (main and settings)
// ============================
const LoggedIn = DrawerNavigator({
  Main: { screen: ListScreens },
  Settings: { screen: SettingsContainer },
});

//... do stuff for root component

What is the best practice of adding hamburger menus to all routes at the DrawerNavigation menu level? I want this pop to open the box. There is no access to props.navigation if I am not inside each of the components ... just state and parameters. Do I need to duplicate the code in each of these files?

static navigationOptions = {
  title: ({ state }) => {
    if (state.params.mode === 'info') {
      return `${state.params.user} Contact Info`;
    }
    return `Chat with ${state.params.user}`;
  },
  header: ({ state, setParams }) => {
    // The navigation prop has functions like setParams, goBack, and navigate.
    let right = (
      <Button
        title={`${state.params.user} info`}
        onPress={() => setParams({ mode: 'info' })}
      />
    );
    if (state.params.mode === 'info') {
      right = (
        <Button
          title="Done"
          onPress={() => setParams({ mode: 'none' })}
        />        
      );
    }
    return { right };
  },
  ..

DOCS HERE

relevant questions (possibly):

https://github.com/react-community/react-navigation/issues/165

+4
source share
2 answers

I created my own header:

  <View style={styles.navBarContainer}>
    <View style={styles.navBarRow}>
      <TouchableHighlight underlayColor={'#COLOR'} onPress={() => {this._drawer.openDrawer()}}>
        <Image source={require('../assets/images/menu-icon.png')} style={styles.menuIcon}/>
      </TouchableHighlight>
      <Text style={styles.navBarText}>{'TITLE'}</Text>
    <View/>
  </View>

, openDrawer(), :

<Drawer
  ref={(drawer) => {this._drawer = drawer}}
  ...
>

:

export default class HomeScreen extends Component {
  static navigationOptions = {
    header: null
  };
  ...
+1

! navigationOptions - Stack Navigation Drawer Navigation

contentComponent Config:
import { DrawerItems, DrawerNavigation } from 'react-navigation'

DrawerItems:

contentComponent: props => <ScrollView><Text>Your Own Header Area Before</Text><DrawerItems {...props} /></ScrollView>.

Title before DrawerItems

DrawerItems:

contentComponent: props => <ScrollView><DrawerItems {...props} /><Text>Your Own Footer Area After</Text></ScrollView>.

0

Source: https://habr.com/ru/post/1670240/


All Articles