React Native:React Navigation StackNavigator 不工作.得到错误:“未定义不是一个对象(评估'this.props.navigation.navigate')"; [英] React Native: React Navigation StackNavigator not working. Getting error: "undefined is not an object (evaluating 'this.props.navigation.navigate')"
问题描述
我正在尝试使用 React Navigation 和 StackNavigator 来浏览我的应用.
I am trying to use React Navigation and StackNavigator to navigate around my app.
我有一个带有 onPress={() => 的按钮导航('DetailsScreen')
,我希望能带我到 DetailsScreen
,但我收到以下错误:
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 不是对象(评估this.props.navigation.navigate")
E ReactNativeJS: undefined is not an object (evaluating 'this.props.navigation.navigate')
我需要添加什么才能使其正常工作?
What do I need to add in order to get this working?
在这里查看我的代码:https://gist.github.com/chapeljuice/bef4b0a4dedef2994c81f36343b81a
推荐答案
您的组件不支持导航(它不是屏幕).因此,您在这里有 2 个常见的解决方案:
You component is not navigation aware (it's not a screen). Hence, you have 2 common solutions here:
从你的父组件(如果它是一个屏幕)传递导航道具.
Pass the navigation prop from your parent component (if it's a screen).
<Card navigation={navigation} />
这是最简单的解决方案.
This is the simplest solution.
如果父组件不支持导航或者传递 props 太复杂,你可以使用 HOC 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))
然后,您将可以使用 navigation
道具.
You will then have the navigation
prop available.
这篇关于React Native:React Navigation StackNavigator 不工作.得到错误:“未定义不是一个对象(评估'this.props.navigation.navigate')";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!