2

I have a custom header for my stack navigation and I want to navigate to another page when I press on an image. But when I press the image I get an error undefined is not an object (evaluating _this.props.navigation.navigate')

In my App.js

const ProfileStackNavi = createStackNavigator({
  stackAndTab:{
    screen:ProfileTopTabNavigator,
    navigationOptions: {
      header: (
        <MyPageTabBarHeader />
      )
    },
  }
})

In my custom header class

export default class MyPageTabBarHeader extends Component {
  constructor(props) {
    super(props)
  }
  render() {
  return(
    <View style={{width:375,height:250, backgroundColor:'white'}}>
          <TouchableOpacity onPress={()=>this.props.navigation.navigate('Register')}>
                    <Image
                    style={{width:23, height:23}}
                    source{require('../Components/Assets/register.png')} />
           </TouchableOpacity>
        </View>
    </View>
    )
  };
}

I also tried <MyPageTabBarHeader navigation={this.props.navigation}/> but then it gives me the error undefined is not an object (evaluating this.props.navigation.navigate') the same error as previously but without an _ before this.

Edited

In my App.js I have createAppContainer. It looks like this:

const StartSwitchNavigator = createStackNavigator(
  {
    one:{
    screen:screenOne,
   },
 {
    two:{
    screen:ProfileStackNavi,
   }
);

const App = createAppContainer(StartSwitchNavigator);

export default App;
3
  • Did you included the 'Register' screen in stack Commented Jul 11, 2019 at 3:06
  • Have you created App container using createAppContainer. Please show your whole code. Commented Jul 11, 2019 at 4:44
  • Yes i did it. I edited my code please check :) Commented Jul 11, 2019 at 5:38

2 Answers 2

4

the navigation isnt being passed properly. try passing it this way :

const ProfileStackNavi = createStackNavigator({
  stackAndTab:{
    screen:ProfileTopTabNavigator,
    navigationOptions: ({navigation}) => ({
      header: <MyPageTabBarHeader navigation= {navigation} />,
    })
  }
})

hope it helps!

Sign up to request clarification or add additional context in comments.

Comments

0

It seems the navigation prop isn't being passed down, that's normal for a custom header. The prop is usually only passed to screens/views. You simply have to import withNavigation from 'react-navigation' in your header component and then export default withNavigation(MyPageTabBarHeader) at the bottom of the file instead of when you declare the class.

You will then have access to the navigation prop. Here's some doc about the function.

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.