最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

reactjs - state not updating in Expo React Native - Stack Overflow

programmeradmin2浏览0评论
  useEffect(() => {
    const interval = setInterval(async () => {
      let { status } = await Location.requestForegroundPermissionsAsync();
      if (status !== 'granted') {
        setErrorMsg('Permission to access location was denied');
        setIsLoaded(true);
        return;
      }

      let location = await Location.getCurrentPositionAsync({});
      setGeoPos((prevState:position) => ({
        ...prevState,
        latitude: location.coords.latitude,
        longitude: location.coords.longitude,
      }));
      setIsLoaded(true);
    }, 500); 
    return () => clearInterval(interval);
  }, []); 

I have this code that fetches the geo position and sets it to a state and then sets the isloaded to true

if (!isLoaded) {
    return (
      <View
        style={{flex:1, alignItems: "center", justifyContent: 'center',}}
      >
        <ActivityIndicator size={"large"} style={{marginBottom: 10,}}></ActivityIndicator>
      </View>
    )
};

This code runs before the main return statement, this shows the activityIndicator until the isLoaded state is true.

The main issue is that I don't understand why the display keeps showing the activity indicator, it doesn't seems to be changing to its loaded state.

By the way, I've used these two exact codes on other components so I don't think the code itself is the main issue imo

发布评论

评论列表(0)

  1. 暂无评论