Search code examples
reactjsreact-nativenavigationreact-props

React Native "Undefined is not an object (evaluating 'this.props')"


I am new to react native. I have created location access file. and now I want to Navigate to "Welcome " Screen. But getting error like this "Undefined is not an object (evaluating 'this.props')"

here is my code of location access file

import React, { useState, useEffect } from 'react';
import { Platform, Text, View, StyleSheet } from 'react-native';
import * as Location from 'expo-location';

export default function App() {
  const [location, setLocation] = useState(null);
  const [errorMsg, setErrorMsg] = useState(null);

  useEffect(() => {
    (async () => {
      let { status } = await Location.requestPermissionsAsync();
      if (status !== 'granted') {
        setErrorMsg('Permission to access location was denied');
        return;
      }

      let location = await Location.getCurrentPositionAsync({});
      setLocation(location);
    })();
  }, []);

  let text = 'Waiting..';
  if (errorMsg) {
    text = errorMsg;
  } else if (location) {
    text = JSON.stringify(location);
    this.props.navigation.navigate("Welcome")
  }

  return (
    <View style={styles.container}>
      <Text style={styles.paragraph}>{text}</Text>
    </View>
  );
}


const styles = StyleSheet.create({
container: {
  flex: 1,
  alignItems: "center",
  justifyContent: "center",
  padding: 20,
},
paragraph: {
  fontSize: 18,
  textAlign: "center",
},
});

Solution

  • ISSUE

    You are using this in functional component

    SOLUTION

    export default function App({navigation}) {
      ....
      if (errorMsg) {
        .....
      } else if (location) {
        ...
        navigation.navigate("Welcome") // use this way
      }
    
      ....
    }