【问题标题】:React Native "Undefined is not an object (evaluating 'this.props')"React Native“未定义不是对象(评估'this.props')”
【发布时间】:2021-01-15 10:50:02
【问题描述】:

我是原生反应的新手。我已经创建了位置访问文件。现在我想导航到“欢迎”屏幕。但是出现这样的错误“未定义不是对象(评估'this.props')”

这是我的位置访问文件代码

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

【问题讨论】:

    标签: reactjs react-native navigation react-props


    【解决方案1】:

    问题

    您在功能组件中使用this

    解决方案

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

    【讨论】:

    • OHHOOOOO 谢谢兄弟,但问题是它需要 2-4 秒等待然后导航。任何想法。谢谢
    • 嘿,它在 Iphone 上工作,但在等待屏幕上卡在 android 上
    猜你喜欢
    • 2020-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-29
    • 2019-04-05
    • 2021-11-09
    • 1970-01-01
    相关资源
    最近更新 更多