0

I want to navigate to List.js after I click the button in MainMenu.js file, but it always shows me this error:

TypeError: undefined is not an object (evaluating 'navigation.push')

This is my App.js file

import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { StyleSheet, Text, View, Button } from 'react-native';
import { List } from './List';
import { MainMenu } from './MainMenu';

export default function App() {
  return (
    <View style={styles.container}>
      <Text>Open up App.js to stassrt working on your app!</Text>
      <StatusBar style="auto" />
      <MainMenu></MainMenu>
    </View>
  );
}

And this is my MainMenu.js file (I have the button in this file):

import React from 'react';
import { Text, Button, View, StyleSheet } from 'react-native';
import { ScreenContainer } from 'react-native-screens';
import { List } from './List';
import { useNavigation } from '@react-navigation/core';


export const MainMenu = ({navigation}) => (
    
    <View>
      <Text>Hello HOW ARE YsOU</Text>
      <Button title="Click" onPress={() => navigation.push("List")}></Button>
    </View>
    
);

2 Answers 2

1

In your MainMenu component, you are expecting an object with a key navigation as props. You are not passing any props to the MainMenu, however, and so the props are undefined, but you are expecting an object. To fix this, change the component to:

import React from 'react';
import { Text, Button, View, StyleSheet } from 'react-native';
import { ScreenContainer } from 'react-native-screens';
import { List } from './List';
import { useNavigation } from '@react-navigation/core';


export const MainMenu = () => {
    const navigation = useNavigation()
    return (
      <View>
        <Text>Hello HOW ARE YsOU</Text>
        <Button title="Click" onPress={() => navigation.push("List")}> 
        </Button>
      </View>
    ); 
};

We are using the hook, instead of props.

Sign up to request clarification or add additional context in comments.

4 Comments

I wrote it like this but now it shows: " Couldn't find a navigation object. Is your component inside a screen in a navigator? "
You didn't set up React Navigation correctly then. You have to include your App inside a screen navigator component for React Navigation to work. I suggest you look at reactnavigation.org/docs/hello-react-navigation, you need to create a stack navigator and wrap your app in it.
I tried to do it exactly as it was written there. It looks like this has no solution 😄
I reinstalled it and it solved it. Thanks :)
0

I don't know how you implemented the stack navigator, but I think your App.js file should look like this:

import { NavigationContainer } from "@react-navigation/native";
import { createStackNavigator } from "@react-navigation/stack";
import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { StyleSheet, Text, View, Button } from 'react-native';
import { List } from './List';
import { MainMenu } from './MainMenu';

const Stack = createStackNavigator();

export default function App() {
return (
   <NavigationContainer>
     <Stack.Navigator>
        <>
         <Stack.Screen name="Main Menu" component={MainMenu} />
         <Stack.Screen name="List" component={List} />
       </>
    </Stack.Navigator>
  </NavigationContainer>
 );
}

And I think your MainMenu component should look like this:

import React from 'react';
import { Text, Button, View, StyleSheet } from 'react-native';
import { ScreenContainer } from 'react-native-screens';
import { List } from './List';

export const MainMenu = ({navigation}) => (
  <View style={styles.container}>
    <Text>Open up App.js to stassrt working on your app!</Text>
    <StatusBar style="auto" />
    <View>
     <Text>Hello HOW ARE YsOU</Text>
     <Button title="Click" onPress={() => navigation.navigate("List")</Button>      
    </View>
  </View>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.