본문 바로가기

React-Native

[solved]undefined is not an object(evaluating 'navigation.navigate')

반응형
※변경전
const StackNavigator = ({navigation}) =>{
    return (
        <Stack.Navigator initialRouteName='MainPage'
            screenOptions={{
                headerStyle: {
                    backgroundColor: "white",
                    borderBottomColor: "white",
                    shadowColor: "white",
                   
                },
                headerTitleAlign:"Left",
                headerTintColor: "#000",
                headerBackTitleVisible: false,
                headerRight:()=>(
                   
                    <View style={{flexDirection:"row", marginHorizontal:10}}>
                    <TouchableOpacity onPress={()=>{navigation.navigate("SearchPage");}}><Fontisto style={styles.fontImg}name='search'/></TouchableOpacity>
                    <TouchableOpacity onPress={link}><Fontisto style={styles.fontImg} name='instagram'/></TouchableOpacity>
                    </View>
                    )

            }}  
        >
        >

▼변경후

 

 
const StackNavigator = () =>{
    return (
        <Stack.Navigator initialRouteName='MainPage'
            screenOptions={({navigation})=>({         //navigation을 screenOptions 함수안에 넣어준다
                headerStyle: {
                    backgroundColor: "white",
                    borderBottomColor: "white",
                    shadowColor: "white",
                   
                },
                headerTitleAlign:"Left",
                headerTintColor: "#000",
                headerBackTitleVisible: false,
                headerRight:()=>(
                   
                    <View style={{flexDirection:"row", marginHorizontal:10}}>
                    <TouchableOpacity onPress={()=>{navigation.navigate("SearchPage");}}><Fontisto style={styles.fontImg}name='search'/></TouchableOpacity>
                    <TouchableOpacity onPress={link}><Fontisto style={styles.fontImg} name='instagram'/></TouchableOpacity>
                    </View>
                    )

            })}  
        >

 

도움을 받은 StackOverflow link
https://stackoverflow.com/questions/62127078/react-navigation-5-how-to-navigate-from-headerright

LIST