React-Native 中 ActivityIndicator 的状态设置在 IOS 上无法正常工作

State setting for ActivityIndicator in React-Native is not working properly on IOS

提问人:Abinesh Abi 提问时间:11/17/2023 最后编辑:Drew ReeseAbinesh Abi 更新时间:11/18/2023 访问量:40

问:

这是我使用的模型,ActivityIndicator

{/* loading Model */}
<Modal
  visible={isLoading}
  animationType="fade"
  transparent={true}
  style={styles.modal}
>
  <View style={styles.modalContainer}>
    {isLoading && <ActivityIndicator size="large" color="#E85C33" />}
  </View>
</Modal>

这是我的 API 调用的 and 函数:useEffect

const [childrenData, setChildrenData] = useState([]);
const [err, setErr] = useState(false)
const [isLoading, setIsLoading] = useState(false);
const [isFound, setIsFound] = useState(false);


const fetchData = async () => {
  let bodyFormData = new FormData()

  bodyFormData.append('ParentID', parentID)
  bodyFormData.append('ParentSchoolID', ParentSchoolID)

  let response = await axios({
    method: 'post',
    url: myapiUrl',
    data: bodyFormData,
    dataType: 'json',
    headers: { "Content-Type": "multipart/form-data" }
  })
    .then( (res) => {
      if (res.data.status == true) {
        // setTimeout(() => {
        setIsLoading(false)
        // }, 1000);
     
        setChildrenData(res.data.result);
         
      } else {
        setIsFound(true);
        setTimeout(() => {
          setIsLoading(false);
        }, 1000);
      }
    })
    .catch((err) => {
      setIsLoading(false);
      setIsFound(true);
      setErr(true);
      console.log('Error fetching data:', err.message);
    });
};
 
console.log('isLoading value :', isLoading);

useEffect(() => {
  setIsLoading(true);

  fetchData();
}, []);

我的问题是我在 API 调用之前将 isLoading 值设置为 true,之后根据结果将其设置为 false,而控制台我得到的值为 false,但模型没有关闭,即使在其他数据设置后也没有停止状态并在后台显示。它在 Android 设备上运行良好,在 Android 上没有问题,但在 iOS 中它的工作方式是这样的

  • 我尝试在 API 调用函数中设置值 true。isLoading
  • 我尝试了方法。.then
  • 我尝试了其他方法,即当状态发生变化时,设置加载 false,它也无济于事。useEffectChildrenData
  • 我使用 1 到 2 秒的延迟将其设置为 false,它在大多数情况下都有效,但有时也不会停止setTimeout
reactjs react-hooks 跨平台 uiactivityindicatorview

评论


答: 暂无答案