【问题标题】:React Native getParam() function is undefinedReact Native getParam() 函数未定义
【发布时间】:2020-03-22 01:15:40
【问题描述】:

我正在尝试使用导航方法将变量从页面传递到另一个页面,但是当我尝试如下所示读取它时,我得到 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-native navigation expo


    【解决方案1】:

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

    function HomeScreen({ navigation, route }) {
     const text = route.params?.text;
    
     return <View>...</View>
    }
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 与现有和接受的答案相比,此答案提供的额外见解是什么?请注意,仅提供链接的任何内容均不被视为此处的答案。
      猜你喜欢
      • 2019-12-03
      • 1970-01-01
      • 1970-01-01
      • 2017-12-06
      • 1970-01-01
      • 2019-02-17
      • 2019-03-22
      • 2020-04-14
      • 1970-01-01
      相关资源
      最近更新 更多