Home > database >  [React Native, RN]TypeError: undefined is not an object (evaluating 'route.params')
[React Native, RN]TypeError: undefined is not an object (evaluating 'route.params')

Time:12-15

    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>
       );
};

I want to use const name in function GameScreen_1. But only thing I got is an error code 'TypeError: undefined is not an object (evaluating 'route.params')' Please help me. I spend many days to solve this problem.

CodePudding user response:

It should be

function GameScreen_1({ navigation, route})

You have placed it inside another Curley brackets.

CodePudding user response:

Your function body should look like that

function GameScreen_1({ route, navigation }){

Here the docs https://reactnavigation.org/docs/params/

  • Related