3

I have a drawer navigator with 2 stacks inside. I would like the first screen of each stack to display the hamburger icon in the header, but when moving to the second screen in a stack I want the hamburger to be replaced with a back button.

How do I connect the drawer to this toggle button and also have it only show up for the first screen in the stack?

When editing my first screens in the stack to include a toggle button I get no response. I followed the process detailed in

Add hamburger button to React Native Navigation

This is my best attempt:

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

/--------------Screens-----------------------/

class FirstScreen extends React.Component {
  static navigationOptions = function(props) {
    return {
      title: '',
      headerLeft: <Button onPress={() => props.navigation.navigate('DrawerNavigator')} title= "=" />
    }
  };
  render() {
    return (
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center', backgroundColor:'teal' }}>
        <Text style={styles.text}>FirstScreen</Text>
        <Button
          title="Next"
          onPress={() => this.props.navigation.navigate('Second')}
        />
      </View>
    );
  }
}

class SecondScreen extends React.Component {
  render() {
    return (
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 
'center', backgroundColor:'pink' }}>
        <Text>Second Screen</Text>
        <Button
      title="Next"
      onPress={() => this.props.navigation.navigate('Third')}
        />
      </View>
    );
  }
}

class ThirdScreen extends React.Component {
  render() {
    return (
      <View style={{ flex: 1, alignItems: "center", justifyContent: "center", backgroundColor:'coral'  }}>
        <Text>Third Screen</Text>
        <Button
          title="Next"
          onPress={() => this.props.navigation.navigate('First')}
        />
      </View>
    );
  }
}

class FourthScreen extends React.Component {
  static navigationOptions = function(props) {
    return {
      title: '',
      headerLeft: <Button onPress={() => props.navigation.navigate('DrawerNavigator')} title= "=" />
    }
  };
  render() {
    return (
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center', backgroundColor:'orange' }}>
        <Text style={styles.text}>4th Screen</Text>
        <Button
          title="Next"
          onPress={() => this.props.navigation.navigate('Fifth')}
        />
      </View>
    );
  }
}

class FifthScreen extends React.Component {
  render() {
    return (
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 
'center', backgroundColor:'purple' }}>
        <Text>5th Screen</Text>
        <Button
          title="Next"
          onPress={() => this.props.navigation.navigate('Sixth')}
        />
      </View>
    );
  }
}

class SixthScreen extends React.Component {
  render() {
    return (
      <View style={{ flex: 1, alignItems: "center", justifyContent: "center", backgroundColor:'lightblue'  }}>
        <Text>6th Screen</Text>
        <Button
          title="Next"
          onPress={() => this.props.navigation.navigate('Fourth')}
        />
      </View>
    );
  }
}

/---------------Stack Navigators-------------/

const StackNavigator1 = createStackNavigator({
  First: {screen: FirstScreen},
  Second: {screen: SecondScreen},
  Third: {screen: ThirdScreen}
});

const StackNavigator2 = createStackNavigator({
  Fourth: {screen: FourthScreen},
  Fifth: {screen: FifthScreen},
  Sixth: {screen: SixthScreen}
});

/--------------- Drawer Navigator-------------/

const DrawerNavigator =  createDrawerNavigator(
  {
    StackNavigator1: {
      screen: StackNavigator1
    },
   StackNavigator2: {
      screen: StackNavigator2
    }
  }
);


export default createAppContainer(DrawerNavigator);


const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});
Bryan Campbell
  • 285
  • 3
  • 9

1 Answers1

0

You can try doing something like this

Add the custom component as Header Title to each of the first stack navigators

const LogoTitle = props => {
    return (
      <TouchableOpacity
        style={{flexDirection: 'row'}}
        onPress={() => navigation.dispatch(DrawerActions.openDrawer())}>
        <Entypo name={'menu'} size={25} color={'black'} />
        <Text style={{fontSize: 18, fontWeight: '400', marginLeft: 10}}>
          Home
        </Text>
      </TouchableOpacity>
    );
  };


<Stack.Screen
        options={{
          headerTitle: props => <LogoTitle {...props} />,
        }}
        name={'Home'}
        component={HomeScreen}
      />

You will get the navigation object as props to stack navigators.

Hope it gives some idea.