0

I'm working on the TouchaleOpacity with onPress to navigate to screen called 'Infocli', but isn't work ... (onPress={() => navigation.navigate('Infocli')}) That error TypeError: undefined is not an object (evaluating 'navigation.navigate')

App.js

import React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import Home from './telas/Home';
import Login from './telas/Login';
import Clientes from './telas/Clientes';
import Pedidos from './telas/Pedidos';
import Produtos from './telas/Produtos';
import Sincro from './telas/Sincro';
import Cadastro from './telas/Cadastro';
import Infocli from './telas/Infocli';

export default function App() {

  const Stack = createStackNavigator();

  return (
    <NavigationContainer>
      <Stack.Navigator initialRouteName='Login'>
        <Stack.Screen options={{headerShown:false}} name='Home' component={Home}  />
        <Stack.Screen options={{headerShown:false}} name='Login' component={Login}  />
        <Stack.Screen options={{headerShown:false}} name='Clientes' component={Clientes}  />
        <Stack.Screen  name='Infocli' component={Infocli}  />
        <Stack.Screen  name='Pedidos' component={Pedidos}  />
        <Stack.Screen options={{headerShown:false}} name='Produtos' component={Produtos}  />
        <Stack.Screen options={{headerShown:false}} name='Cadastro' component={Cadastro}  />
        <Stack.Screen name='Sincro' component={Sincro}  />
      </Stack.Navigator>
    </NavigationContainer>
  );
}

Screen where hapness the error

import React, {useState} from 'react';
import { TouchableOpacity, View, Image, Text, StyleSheet } from 'react-native';

export default function ListItem ({ data, navigation}) {


  return (

      <TouchableOpacity 
        onLongPress={() => {alert('FUNFOU !')}} style={styles.item}
        onPress={() => navigation.navigate('Infocli')}
      >
        <View style={{flexDirection:'row',justifyContent:'flex-start', alignItems:'center'}}>
          <Image source={require('../../assets/spartan.png')} style={styles.itemPhoto} />
          <View style={styles.itemInfo}>
            <Text style={styles.itemP1}>{data.cod} - {data.name}</Text>
            <Text style={styles.itemP2}>{data.cnpj_cpf}</Text>
            <Text style={styles.itemP2}>{data.cidade} - {data.estado}</Text>
          </View>
        </View>
      </TouchableOpacity>
      
  );
};

enter image description here

Andrew Lohr
  • 5,380
  • 1
  • 26
  • 38
  • Does this answer your question? [Getting undefined is not an object evaluating \_this.props.navigation](https://stackoverflow.com/questions/45226391/getting-undefined-is-not-an-object-evaluating-this-props-navigation) A little research never killed nobody – Mihai T Aug 13 '21 at 17:47
  • if this answer had solved my problem, I wouldn't have made a post asking if anyone could help me. – Pedro Henrique Aug 13 '21 at 18:00
  • This problem seems like it has a lot of possible solutions both in the answers section of that question and also in other answers across the web. I am sure you will find a solution. With the amount of debugging details you shared it's impossible for us to know which of those solutions might work for you. So either do more research and try to solve it on your own, either share what you have tried to solve the problem and didn't succeed so we can narrow down the possible causes. – Mihai T Aug 13 '21 at 18:03
  • @MihaiT I disagree, I don't see any problem with Pedro's question needing help (see my answer below). – Hugo Bounoua Aug 13 '21 at 18:10
  • @HugoBounoua I disagree. As you basically have shown in your pseudo-answer ( which is more of a comment ) the amount of debugging details shared by the OP is not enough for us to figure out which is the cause of the problem. Otherwise you wouldn't have used a question in your answer. – Mihai T Aug 13 '21 at 18:18

1 Answers1

3

I assume it's not a page you can navigate to, since it's not in the list of your Navigator. So you might just have to pass 'navigation' as a property when you integrate the component 'ListItem' in your page

EDIT : (see comments below)

You need to pass 'navigation' as a parameter to your component in order to use the prop 'navigation' inside 'ListItem'.

Currently you are not passing it, that's why your prop 'navigation' is null and you get your error. You should do :

<ListItem data={item} navigation={navigation} />
Hugo Bounoua
  • 415
  • 4
  • 10
  • Thanks for try to help me !! i use this component inside a flatlist } keyExtractor={(item) => item.id} /> – Pedro Henrique Aug 13 '21 at 18:16
  • Great! You have your answer then. You need to pass 'navigation' as a parameter to your component in order to use the prop 'navigation' inside 'ListItem'. Currently you are not passing it, that's why your prop 'navigation' is null and you get your error. You should do : Don't hesitate to validate this answer if that helped you. – Hugo Bounoua Aug 13 '21 at 18:29