开发者问题收集

[React Native,RN]TypeError:未定义不是对象(评估“route.params”)

2021-12-14
357
import React, {useState, Component, useMemo} from 'react';
    import { StyleSheet, Text, View, Button, TextInput, Image } from 'react-native';
    import { NavigationContainer } from '@react-navigation/native';
    import AsyncStorage from '@react-native-async-storage/async-storage'
    import { createStackNavigator } from '@react-navigation/stack';


    const Stack = createStackNavigator();

    function GameScreen({   navigation, route  }){
const [name, setName] = useState('이화연');
return(
       <View style={{paddingTop:10}}>
       <View style={{flexDirection:"column"}}>
       <Text style={text_st}>당신의 이름을 알려 주세요.</Text>
       <TextInput style={input_st}
       onChangeText={setName}
       value={name}
       maxLength={10}/>
       <Button title="확인"
       color= {EwhaGreen}
       onPress={function(){navigation.navigate('1',
                                               { params: name } )}} />
       </View>
       </View>
       );
};

    function GameScreen_1({ navigation }, {route}){
var N = route.params.name
return(
       <View style={{alignItems:'center', justifyContent:'center'}}>
       <View style={{margin:5}}></View>
       <div>
       <Text>{N}, 일어나!</Text>
       </div>
       <Text>눈을 떠 보니 여기는...?</Text>
       <View style={{margin:5}}></View>
       <Image style={{width:500, height:500}} source={require('./assets/1.jpg')} />
       <View style={{margin:10}}></View>
       <Button title="이화여대?"
       color= {EwhaGreen}
       onPress={function(){navigation.navigate('2') }} />
       </View>
       );
};

我想在 GameScreen_1 函数中使用 const 名称。但我只收到错误代码“TypeError:undefined 不是对象(评估“route.params”)”请帮助我。我花了很多天来解决这个问题。

2个回答

它应该是

function GameScreen_1({ navigation, route})

您已将其放在另一个 Curley 括号内。

Ravi
2021-12-14

你的函数主体看起来应该像这样

function GameScreen_1({ route, navigation }){

这里是文档 https://reactnavigation.org/docs/params/

Thomas Dittmar
2021-12-14