React Native: React Navigation StackNavigator not working. Getting error: “undefined is not an object (evaluating 'this.props.navigation.navigate')”

微笑、不失礼 提交于 2019-12-19 04:50:12

问题


I am trying to use React Navigation and StackNavigator to navigate around my app.

I have a button with onPress={() => navigate('DetailsScreen'), and I was hoping that would take me to the DetailsScreen, but I'm getting the following error:

E ReactNativeJS: undefined is not an object (evaluating 'this.props.navigation.navigate')

What do I need to add in order to get this working?

See my code here: https://gist.github.com/chapeljuice/bef4b0a4dedef2994c81f3634b81aa43


回答1:


You component is not navigation aware (it's not a screen). Hence, you have 2 common solutions here:

Use the parent

Pass the navigation prop from your parent component (if it's a screen).

<Card navigation={navigation} />

This is the simplest solution.

Use the Higher-Order component withNavigation

If the parent component is not navigation aware or if it's too complex to pass down the props, you can use the HOC withNavigation:

export default withNavigation(connect(mapStateToProps)(Card))

You will then have the navigation prop available.



来源:https://stackoverflow.com/questions/44029208/react-native-react-navigation-stacknavigator-not-working-getting-error-undef

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!