React Native:React Navigation StackNavigator无效。得到错误:“undefined不是一个对象(评估'this.props.navigation.navigate')”

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


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

我正在尝试使用React Navigation和StackNavigator来浏览我的应用程序。

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:

我有一个带onPress = {()=> navigate('DetailsS​​creen')的按钮,我希望这会带我进入DetailsS​​creen,但是我收到以下错误:

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

E ReactNativeJS:undefined不是对象(评估'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

在这里查看我的代码:https://gist.github.com/chapeljuice/bef4b0a4dedef2994c81f3634b81aa43

1 个解决方案

#1


4  

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:

如果父组件不能识别导航,或者它太复杂而无法传递道具,则可以使用HOC withNavigation:

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

You will then have the navigation prop available.

然后,您将获得导航道具。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2017/05/17/2c2541f3d44373425208b55372b9c8e6.html



 
  © 2014-2022 ITdaan.com