Adoga Patricia
Adoga Patricia

Reputation: 129

How do I solve react native navigation.navigate error

It's my first time using react native and I want to move to another screen (slide2), I have an error which reads "Undefined is not an object (evaluating navigation.navigate)" I am kind of stuck, this is how far I have gone. Please also explaining a bit will be very much appreciated and showing me what to add and where, thank you

slideOne.js page code

import 'react-native-gesture-handler';
import {NavigationContainer} from '@react-navigation/native';
import {createStackNavigator} from '@react-navigation/stack';
import React from 'react';
import SlideTwo from './SlideTwo';

import {
  SafeAreaView,
  StyleSheet,
  ScrollView,
  View,
  Text,
  StatusBar,
  TextInput,
  Button,
} from 'react-native';

import {
  Header,
  LearnMoreLinks,
  Colors,
  DebugInstructions,
  ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';

function SlideOne({navigation}) {
  return (
    <React.Fragment>
      <View style={styles.body}>
        <View style={styles.wrapper}>
          <View style={styles.imageWrap}></View>

          <TextInput
            placeholder="What should we refer to you as?"
            placeholderTextColor="#03444F60"
            style={styles.textInput}
            underlineColorAndroid="transparent"
          />
        </View>
        <View style={styles.label}>
          <Text style={styles.labelText}>First Name</Text>
        </View>

        <View style={styles.textWrap}>
          <Text style={styles.text}>Back</Text>
          <Text style={styles.text}>Next</Text>
        </View>
        <Button
          title="Go to screen two"
          onPress={() => navigation.navigate('SlideTwo')}
        />
      </View>
    </React.Fragment>
  );
}
export default SlideOne;


this is my index.js where the routing is

    import 'react-native-gesture-handler';
    import {NavigationContainer} from '@react-navigation/native';
    import {createStackNavigator} from '@react-navigation/stack';
    import {AppRegistry} from 'react-native';
    import App from './App';
    import SlideOne from './SlideOne';
    import SlideTwo from './SlideTwo';
    import {name as appName} from './app.json';
    AppRegistry.registerComponent(appName, () => SlideOne);

app.js code

import React from 'react';
import {createStackNavigator} from '@react-navigation/stack';
import {NavigationContainer} from '@react-navigation/native';

import SlideOne from './SlideOne';
import SlideTwo from './SlideTwo';

// NAVIGATION
const StackNavigator = createStackNavigator();

function App() {
  <NavigationContainer>
    <StackNavigator.Navigator>
      <StackNavigator.Screen name="SlideOne" component={SlideOne} />
      <StackNavigator.Screen name="SlideTwo" component={SlideTwo} />
    </StackNavigator.Navigator>
  </NavigationContainer>;
}

export default App;

Upvotes: 0

Views: 4681

Answers (3)

Mohd. Anas Siddiqui
Mohd. Anas Siddiqui

Reputation: 783

I don't know why this is happening although we are following the official documentation.

I hope the following solution will work for you as it is aslo working for me.

npm i @react-native-community/masked-view @react-navigation/native @react-navigation/stack react-native-gesture-handler react-native-safe-area-context [email protected] react-navigation

then remove node_modules then again install node_modules using

npm i

Upvotes: 0

Awais Rana
Awais Rana

Reputation: 184

Your index.js file is incorrect just replace index.js file with this

import 'react-native-gesture-handler';
import {NavigationContainer} from '@react-navigation/native';
import {createStackNavigator} from '@react-navigation/stack';
import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';
AppRegistry.registerComponent(appName, () => App);

you should register App as AppRegistry component instead you are registering SlideOne

Upvotes: 1

agerxs
agerxs

Reputation: 66

You didn't init your "navigation" const. try this : - Import useNavigation from @react-navigation/native

import { useNavigation } from '@react-navigation/native';
  • And init your navigation const like this

    const navigation = useNavigation();

Upvotes: 0

Related Questions