3

Below is my code which uses drawer navigation. But header given on HomeScreen disappears and is not visible. I am only using drawer navigation and no nesting of navigation is going on.

App.js file -

 import React, { Component } from "react";
import { Platform, StyleSheet, Text, View } from "react-native";
import MyApp from "./src/router/router";

export default class App extends Component {
  render() {
    return <MyApp />;
  }
}

router.js file -

import {
  createDrawerNavigator,
  createAppContainer
} from "react-navigation";
import Sidebar from "../components/Sidebar/Sidebar";
import HomeScreen from "../components/HomeScreen/HomeScreen";

const MyDrawerNavigator = createDrawerNavigator(
  {
    Home: {
      screen: HomeScreen
    }
  },
  {
    contentComponent: Sidebar,
    drawerWidth: 200,

  }
);

const App = createAppContainer(MyDrawerNavigator);

export default App;

HomeScreen.js file -

import React, { Component } from "react";
import {
  Text,
  View,
  ScrollView,
  TextInput,
  TouchableOpacity,
  Image
} from "react-native";
import { Calendar } from "react-native-calendars";
import ham from "../../assets/images/ham.png";

export default class HomeScreen extends Component {
  static navigationOptions = {
    drawerLabel: "Maruti Hotel Management",
    headerStyle: {
      backgroundColor: "#ED5A6C"
    },
    headerTintColor: "#fff",
    headerTitleStyle: {
      fontWeight: "bold",
      flex: 1,
      textAlign: "center"
    }
  };

  state = {
    markedDate: {}
  };

  dateSelectHandler = date => {
    let selectedDate = date.dateString;
    this.setState({
      markedDate: {
        [selectedDate]: {
          selected: true,
          marked: true,
          selectedColor: "#ED5A6C"
        }
      }
    });
  };

  render() {
    return (
      <ScrollView style={{ flex: 1 }}>
        <Calendar
          style={{ flex: 1 }}
          // Initially visible month. Default = Date()
          onDayPress={day => {
            console.log(day);
            this.dateSelectHandler(day);
          }}
          markedDates={this.state.markedDate}
          theme={{
            "stylesheet.calendar.header": {
              week: {
                marginTop: 5,
                flexDirection: "row",
                justifyContent: "space-around",
                backgroundColor: "#ED5A6C",
                color: "red"
              },
              dayHeader: {
                marginTop: 2,
                marginBottom: 7,
                width: 32,
                textAlign: "center",

                color: "#fff"
              }
            },
            calendarBackground: "#F5A5AE",

            arrowColor: "#ED5A6C",
            monthTextColor: "#ED5A6C",
            dayTextColor: "#ED5A6C",
            todayTextColor: "blue"
          }}
        />
        <View
          style={{
            flex: 1,
            backgroundColor: "#F07886",
            alignItems: "center",
            paddingTop: "2%",
            paddingBottom: "10%"
          }}
        >
          <Text
            style={{
              textAlign: "center",
              color: "#FFF",
              fontWeight: "500",
              fontSize: 17
            }}
          >
            Total Income (गल्ला)
          </Text>
          <TextInput
            style={{
              borderBottomColor: "#fff",
              borderBottomWidth: 1,
              paddingRight: "3%",
              paddingLeft: "3%",
              marginBottom: "2%",
              width: "80%"
            }}
            editable={true}
            maxLength={40}
            placeholder="Rs"
          />

          <TouchableOpacity style={{ width: "80%", marginTop: "2%" }}>
            <View
              style={{
                borderRadius: 5,
                backgroundColor: "#D85263",
                paddingTop: 10,
                paddingBottom: 10,
                // paddingLeft: 15,
                // paddingRight: 15,
                justifyContent: "center",
                alignItems: "center"
              }}
            >
              <Text
                style={{
                  width: "80%",
                  textAlign: "center",
                  color: "#fff",
                  fontWeight: "700",
                  fontSize: 16,
                  letterSpacing: 2
                }}
              >
                Submit
              </Text>
            </View>
          </TouchableOpacity>
        </View>
      </ScrollView>
    );
  }
}

Please help as I am stuck and already viewed similar questions but with no luck

2 Answers 2

2

Use generic header component and implement it in each screen where header is required. Pass header name as prop from the screen

Header Component:

class PageHeader extends Component {
        <Header>
          <Left>
            <Button
              transparent
              onPress={() => this.props.navigation.openDrawer()}
            >
              <Icon name="menu" />
            </Button>
          </Left>
          <Body>
            <Title>{this.props.title}</Title>
          </Body>
          <Right />
        </Header>
}
export default PageHeader;

Sample Screen:

<Container style={styles.container}>
        <PageHeader title="Home" />

        <View>
          // screen view
        </View>
      </Container>
Sign up to request clarification or add additional context in comments.

Comments

2

The best way I found to use and customize headers for different screens is by using the Header component by react-native-elements. You just add the component to each screen that you want the header on. Also, dont forget to do header: null on your stackNavigator so it wont show 2 headers on the screen.

Example below:

<React.Fragment>
  <Header
    statusBarProps={{ barStyle: 'light-content' }}
    barStyle="light-content"
    leftComponent={
      <SimpleIcon
        name="menu"
        color="#34495e"
        size={20}
      />
    }
    centerComponent={{ text: 'HOME', style: { color: '#34495e' } }}
    containerStyle={{
      backgroundColor: 'white',
      justifyContent: 'space-around',
    }}
  />
</React.Fragment>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.