我正在try 请求并获得用户的位置.这是我的密码.我正在try 这样做,这样用户一出现在屏幕上,就会被要求提供他的位置,因此使用了useEffect.
我在另一个屏幕上执行了类似的操作来访问用户的图片库,但AccessLocation函数的类似功能只有在用户按下某个按钮时才会被调用,并且它不会抛出该错误.
当函数处于useEffect时,其语法或调用函数的方式是否有所不同?我还在学习react ,所以别对我太客气了.
import { View, Text } from "react-native";
import React, { useEffect, useState } from "react";
export default function DateFinder() {
const [hasForegroundPermissions, setHasForegroundPermissions] =
useState(null);
const [userLocation, setUserLocation] = useState(null);
useEffect(() => {
const AccessLocation = async () => {
function appSettings() {
console.warn("Open settigs pressed");
if (Platform.OS === "ios") {
Linking.openURL("app-settings:");
} else RNAndroidOpenSettings.appDetailsSettings();
}
const appSettingsALert = () => {
Alert.alert(
"Allow Wassupp to Use your Location",
"Open your app settings to allow Wassupp to access your current position.",
[
{
text: "Cancel",
onPress: () => console.warn("Cancel pressed"),
},
{ text: "Open settings", onPress: appSettings },
]
);
};
const foregroundPermissions =
await Location.requestForegroundPermissionsAsync();
if (
foregroundPermissions.canAskAgain == false ||
foregroundPermissions.status == "denied"
) {
appSettingsALert();
}
setHasForegroundPermissions(foregroundPermissions.status === "granted");
if (hasForegroundPermissions == true) {
const location = await Location.getCurrentPositionAsync({
accuracy: Location.Accuracy.Highest,
});
}
};
AccessLocation();
});
return (
<View>
<View>
<Text>"Home, sweet home"</Text>
</View>
</View>
);
const styles = StyleSheet.create({
background: {
backgroundColor: COLORS.background_Pale,
flex: 1,
// justifyContent: "flex-start",
//alignItems: "center",
},
image: {
flex: 1,
// height: null,
// width: null,
//alignItems: "center",
},
scrollView: {
backgroundColor: COLORS.background_Pale,
},
});
}
当我按下警告时,它显示promiseRejectionTrackingOptions.js(43:17).