当我的程序获取天气数据时,我正在尝试制作一个简单的进度计数器,从0%到100%.API请求是由getLocation()内部index.ios.js调用fetchWeather()的weatherApi.js.有没有办法衡量fetch函数对我的API请求的进度?如果没有,实施加载栏的好方法是什么?
weatherAPI.js
const rootUrl ='http://api.openweathermap.org/data/2.5/weather?appid=fcea54d0ceade8f08ab838e55bc3f3c0'
export const fetchWeather = (lat,lon) => {
const url = rootUrl+'&lat='+lat+'&lon='+lon+"&units=metric"
console.log(url)
return fetch(url)
.then(res => res.json())
.then(json => ({
temp: json.main.temp,
weather: json.weather[0].main
}))
}
Run Code Online (Sandbox Code Playgroud)
index.ios.js
import React, {Component} from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
StatusBar
} from 'react-native'
import Icon from 'react-native-vector-icons/Ionicons'
import {fetchWeather} from './weatherAPI'
import Highlight from 'react-native-highlight-words'
const iconNames = {
Default: 'md-time',
Clear: 'md-sunny',
Rain: 'md-rainy',
Thunderstorm: 'md-thunderstorm',
Clouds: …Run Code Online (Sandbox Code Playgroud)