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

删除回忆录丶 提交于 2021-01-29 15:00:53

问题


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",
},
});

回答1:


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
  }

  ....
}


来源:https://stackoverflow.com/questions/65734714/react-native-undefined-is-not-an-object-evaluating-this-props

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!