Uncaught Error: Cannot read property ‘navigate’ of undefined

I’m trying to implement a very simple navigation in react native. I’m trying to navigate from the Home Screen by clicking a button to go to a Details Screen. When clicking the button I get this error: Cannot read property ‘navigate’ of undefined.Here’s my code:

ArtScreen.js (The Details Screen):

import { View, Text, StyleSheet } from 'react-native';


export default function ArtScreen() {
 return (
   <View style={styles.container}>
     <Text style={styles.text}>Welcome to Art</Text>
   </View>
 );
}



const styles = StyleSheet.create({
 container: {
   flex: 1,
   justifyContent: 'center',
   alignItems: 'center',
   backgroundColor: '#fff',
 },
 text: {
   fontSize: 24,
   fontWeight: 'bold',
 },
});

NavScreenReg.js (where I register my screens with the stack navigator and wrap them in the Navigation Container)

import React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import App from './App';
import ArtScreen from './ArtScreen';



const Stack = createNativeStackNavigator();


export default function NavScreenReg() {
 return (
   <NavigationContainer>
     <Stack.Navigator initialRouteName="Home">
       <Stack.Screen name="Home" component={App} />
       <Stack.Screen name="Art" component={ArtScreen} />
     </Stack.Navigator>
   </NavigationContainer>
 );
}

App.js (my Home Screen from which I try to navigate to the Art(Details) Screen)

import React from 'react';
import { View, Text, StyleSheet, Button, Alert } from 'react-native';
//import { useNavigation } from '@react-navigation/native';
//import { createNativeStackNavigator } from '@react-navigation/native-stack';
//import {useNavigation} from '@react-navigation/native'



export default function App({navigation}) {
  //const navigation = useNavigation();

  // Log the navigation prop
  console.log('Navigation prop:', navigation);
  
 return (
   <View style={styles.container}>
     <View style={styles.navBar}>
       <Text style={styles.navItem}>Item1</Text>
       <Text style={styles.navItem}>Item2</Text>
       <Text style={styles.navItem}>Item3</Text>
       <Text style={styles.navItem}>Item4</Text>
     </View>

    
     <View style={styles.blocksContainer}>
       <View style={styles.row}>
         <View style={styles.block}>
           <Text style={styles.blockText}>Art</Text>
           <Button
        title="Go to Details"
        onPress={() => navigation.navigate('Art')}
      />
         </View>
         <View style={styles.block}>
           <Text style={styles.blockText}>Block2</Text>
         </View>
       </View>
       </View>
       <View style={[styles.block, styles.singleBlock]}>
         <Text style={styles.blockText}>Block3</Text>
       </View>
    
   </View>
 );
}


const styles = StyleSheet.create({
 container: {
   flex: 1,
   backgroundColor: '#fff',
 },
 navBar: {
   marginTop: 30,
   flexDirection: 'row',
   justifyContent: 'space-between',
   alignItems: 'center',
   backgroundColor: '#f8f8f8',
   height: 60,
   paddingHorizontal: 20,
   borderBottomWidth: 1,
   borderBottomColor: '#e0e0e0',
 },
 navItem: {
   fontSize: 18,
   fontWeight: 'bold',
   color: '#333',
 },
 descriptionText: {
   fontSize: 16,
   color: '#555',
   marginTop: 20,
   marginHorizontal: 20,
   textAlign: 'center',
 },
 blocksContainer: {
   marginTop: 40,
   paddingHorizontal: 20,
 },
 row: {
   flexDirection: 'row',
   justifyContent: 'space-between',
   marginBottom: 10,
 },
 column: {
   flexDirection: 'column',
   justifyContent: 'space-between',
   marginBottom: 10,
 },
 block: {
   flex: 1,
   height: 100,
   backgroundColor: '#f0f0f0',
   justifyContent: 'center',
   alignItems: 'center',
   margin: 5,
   borderRadius: 8,
 },
 singleBlock: {
   alignSelf: 'center',
   width: '95%',
 },
 blockText: {
   fontSize: 16,
   fontWeight: 'bold',
   color: '#555',
 },
});

When I try to log the value of the navigation prop I get ‘undefined’ even though the navigation prop is passed to the home screen and both my home screen and my details screen are registered with the stack navigator and wrapped in a navigation container. I made sure to install the necessary dependencies: npm install @react-navigation/native
npx expo install react-native-screens react-native-safe-area-context
I also tried using the hook useNavigation instead of using the navigation prop and that was giving me an error as well, but a different one.

New contributor

Anastasija Stojanovska is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật