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 联系我们: