反应本机 "Undefined is not an object (evaluating 'this.props')"
React Native "Undefined is not an object (evaluating 'this.props')"
我是 React Native 的新手。我已经创建了位置访问文件。现在我想导航到“欢迎”屏幕。但是出现这样的错误“Undefined is not an object (evaluating '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",
},
});
问题
您正在功能组件
中使用this
解决方案
export default function App({navigation}) {
....
if (errorMsg) {
.....
} else if (location) {
...
navigation.navigate("Welcome") // use this way
}
....
}
我是 React Native 的新手。我已经创建了位置访问文件。现在我想导航到“欢迎”屏幕。但是出现这样的错误“Undefined is not an object (evaluating '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",
},
});
问题
您正在功能组件
中使用this
解决方案
export default function App({navigation}) {
....
if (errorMsg) {
.....
} else if (location) {
...
navigation.navigate("Welcome") // use this way
}
....
}