你不可能
useState()
在一个
React.useEffect()
.
将这些语句移到useEffect的顶部和外部
function OfferScreen({ navigation }: { navigation: any }) {
//STATE VARIABLES FOR FETCHING DATA
const [loading, setLoading] = useState(true);
const [data, setData] = useState([]);
const value = new Animated.Value(1);
//GEOLOCATION TEST
//STATE FOR GEOLOCATION
const [location, setLocation] = (useState < any) | (null > null);
const [hasPermission, setHasPermission] = (useState < any) | (null > null);
React.useEffect(() => {
if (!loading && data) {
(async () => {
const { status } = await Location.requestForegroundPermissionsAsync();
setHasPermission(status === "granted");
let location = await Location.getCurrentPositionAsync({});
})();
}
}, [data, loading]);
//FETCHING DATA
React.useEffect(() => {
fetch("https://fidness.net/WSExchange/getListActiveProspectus")
.then((response) => response.json())
.then((json) => {
setData(json);
setLoading(false);
})
.catch((error) => {
alert(
"Erreur avec le chargement des offres, veuillez réssayer ultérieurement!"
);
setLoading(false);
});
}, []);
}
export default OfferScreen;