React Native getParam() 函数未定义

React Native getParam() function is undefined

我正在尝试使用导航方法将变量从一个页面传递到另一个页面,但是当我尝试如下所示读取它时,我得到 getParam() 方法未定义。我没有使用 类.

这是我传递变量的第一个页面

const handleBarCodeScanned = ({ type, data }) => {
setScanned(true);
//alert(`Bar code with type ${type} and data ${data} has been scanned!`);
splitted = data.split('=');
invitationFromURL=splitted[1];
navigation.navigate('HomeScreen',{text:invitationFromURL}); 


//console.log('Invite '+splitted[1]); 



}; 

这是我收到变量的主页

import { Image, Platform, StyleSheet, Text, TouchableOpacity, View, Button,Alert } from 'react-native';
import { ScrollView } from 'react-native-gesture-handler';
//import { IconButton, Colors } from 'react-native-paper';
import * as WebBrowser from 'expo-web-browser';


import { NavigationContainer } from '@react-navigation/native'
import { createStackNavigator } from '@react-navigation/stack'


//import { BarCodeScanner } from 'expo-barcode-scanner';

import { MonoText } from '../components/StyledText';






export default function HomeScreen(props) {
const { navigation } = props

const text = navigation.getParams ('text','nothing sent');

我得到的错误
** navigation.getParams 不是函数。 (在'navigation.getParams('text','nothing sent')'中,'navigation.getParams'未定义)**

在 react-navigation 版本 5 中,你应该像这样使用 route 道具:

function HomeScreen({ navigation, route }) {
 const text = route.params?.text;

 return <View>...</View>
}

我认为你应该将你的 react-navigation v4 升级到 v5 并使用 route.params?.someParam ?? 'defaultValue';

阅读文档 :=> https://reactnavigation.org/docs/upgrading-from-4.x/#no-more-getparam