0

I am trying to make an onPress function such that when the menu icon is clicked, my drawer navigation will open. However, I am getting this error: Unhandled JS Exception: Cannot read property 'navigation' of undefined.

Here's my code for HeaderComponent.js

import React from 'react';
import { StyleSheet } from 'react-native';
import { Header, Left, Icon, Body, Title, Right } from 'native-base';

const HeaderComponent = (props) => {
const { menuIconStyle } = styles;

return (
    <Header>
        <Left>
            <Icon
                style={menuIconStyle}
                name="menu"
                onPress={() => this.props.navigation.openDrawer()}
            />
        </Left>
        <Body>
            <Title>{props.headerText}</Title>
        </Body>
        <Right>

        </Right>
    </Header>
);
}

export default HeaderComponent;

const styles = StyleSheet.create({
menuIconStyle: {
    paddingLeft: 15,
},
});

Would appreciate a solution. Thanks!

user7381027
  • 31
  • 1
  • 10

2 Answers2

1

You should pass navigation props from parent to child,like this:

<HeaderComponent navigation={this.props.navigation} />

then you can use navigation in your child component

Reza Madani
  • 354
  • 1
  • 5
0

Your component is not a ES6 class but a function. So, you cannot use this.props because it doesn't exist.

onPress={() => props.navigation.openDrawer()}

use this instead.