Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

268
Visualizações
TypeError: undefined is not an object (evaluating '_this.props.navigation') in React Native

I'm trying to work with React Navigation using an external button component, to make styling it easier, but it gives me the TypeError: undefined is not an object (evaluating '_this.props.navigation') error when I try to pass the navigation. If I create the button on my home screen it works fine, but that clutters up my HomeScreen's stylesheet, and means I have to repeat code when I use the button elsewhere.

I have the stack navigation set up in my App.js, and again, it works fine when I'm not trying to pass the navigation as a prop.

Here's HomeScreen.js

import React from "react";
import {
    ScrollView,
    StyleSheet,
    Text,
    View,
    AsyncStorage,
  } from 'react-native';

import Heading from '../heading';
import Input from '../Input';
import Button from "../Button";

export const Home = ({navigation}) => (
        <View style={styles.container}>
            <ScrollView style={styles.content}>
                <Heading />
            </ScrollView>
            <Button/>
        </View>
)

And here's my Button.js

/* eslint-disable prettier/prettier */
import React from 'react';
import {
    StyleSheet, 
    Text, 
    View, 
    TouchableHighlight 
} from 'react-native';
import { useNavigation } from '@react-navigation/native';


function Button(){
    const navigation = useNavigation()
    return(
    <View style={styles.buttonContainer}>
        <TouchableHighlight
        underlayColor='rgba(175, 47, 47, .75)'
        activeOpacity={1.0}
        style={styles.button}
        onPress={() => {
        navigation.navigate("New Medication");
          }}>   
            <Text style={styles.submit}>
            +
            </Text>
        </TouchableHighlight>
    </View>
    )
}

As far as I understand it, this should work just fine, so why is it saying that it's being passed as undefined?

UPDATE: Thanks to some suggestions I now have it to where it doesn't give an error, just does nothing.

Here's my App.js, with my navigation

const Stack = createStackNavigator();
function MyStack() {
  return (
    <Stack.Navigator 
    screenOptions={{
      headerShown: false
    }}>
      <Stack.Screen name="Home" component={Home} />
      <Stack.Screen name="NewMedication" component={newMedScreen} />
    </Stack.Navigator>
  );
}

class App extends Component{
  
  render(){
    return(
      <NavigationContainer>
        <MyStack />
      </NavigationContainer>
    );
  }
}

export default App;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I recommend you to create a const with screen name and reuse it everywhere to avoid such problem. You nave stack like this <Stack.Screen name="NewMedication" component={newMedScreen} /> , but try to navigate navigation.navigate("New Medication").Of course this screen do not exist

const NEW_MEDICATION_SCREEN = 'NewMedication'
 <Stack.Screen name={NEW_MEDICATION_SCREEN} component={newMedScreen} />
.....

onPress={() => navigation.navigate(NEW_MEDICATION_SCREEN)}>   

and for make Button reusable use it like this

function Button({onPress}){
  return(
    <View style={styles.buttonContainer}>
      <TouchableHighlight
        underlayColor='rgba(175, 47, 47, .75)'
        activeOpacity={1.0}
        style={styles.button}
        onPress={onPress}>   
          <Text style={styles.submit}>
            +
          </Text>
      </TouchableHighlight>
    </View>
)}

export const Home = ({navigation}) => (
    <View style={styles.container}>
        <ScrollView style={styles.content}>
            <Heading />
        </ScrollView>
        <Button onPress={() => navigation.navigate(NEW_MEDICATION_SCREEN)}/>
    </View>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda