【问题标题】:get weather from weather api and setState inoto array of objects in react native从天气 api 和 setState inoto 对象数组中获取天气反应本机
【发布时间】:2020-07-10 12:13:09
【问题描述】:

这是我的状态。我想从state 中的所有对象中获取latlong 并获取它们当前的天气,然后将它们的temperature 设置为温度状态。有人可以帮我解决这个问题吗?

class App extends React.Component{
        this.state = {
              data: [
                {
                  id: 1,
                  lat: "35.6892" ,
                  long: "51.3890",
                  temperature: '',
                },
                {
                  id: 2,
                  lat: "45.6892" ,
                  long: "35.3890",
                  temperature: '',
                },
                {
                  id: 3,
                  lat: "59.6892" ,
                  long: "-72.3890",
                  temperature: '',
                },
                {
                  id: 4,
                  lat: "23.6892" ,
                  long: "-52.3890",
                  temperature: '',
                },
        
            componentDidMount() {
                
                for(var i =0 ; i<= this.state.data.length - 1 ; i++) {
            
                   let url = 'https://api.openweathermap.org/data/2.5/weather?lat=' + this.state.data[i].lat + '&lon=' + this.state.data[i].long + '&units=metric&appid=api key';
                
                  fetch(url)
                    .then(response => response.json())
                    .then(data => {
                        this.setState.data((prevState, props) => ({
                            temperature: data.main.temp
                    }));
                    })
                }
              }
    }

【问题讨论】:

    标签: react-native fetch setstate weather-api


    【解决方案1】:

    虽然在 for 循环中使用 fetch 请求从来都不是一个好主意,因为如果元素数量增加了,就会有大量的 fetch 请求到服务器,这最终会使进程变慢。如果你想这样做,试试下面描述的代码:

    for(var i =0 ; i<= this.state.data.length - 1 ; i++) {
            
        let url = 'https://api.openweathermap.org/data/2.5/weather?lat=' + this.state.data[i].lat + '&lon=' + this.state.data[i].long + '&units=metric&appid=api key';
                
        fetch(url)
        .then(response => response.json())
         .then(data => {
              // 1. Make a shallow copy of the items
              let items = [...this.state.data];
    
              // 2. Make a shallow copy of the item you want to mutate
              let item = {...items[1]};
    
              // 3. Replace the property you're intested in
              item.temperature = data.main.temp;
    
              // 4. Put it back into our array. N.B. we *are* mutating the array here, but that's why we made a copy first
              items[1] = item;
    
               // 5. Set the state to our new copy
               this.setState({items});
          })
       }
    

    【讨论】:

    • 感谢您的回复。我想要做的是我的州有一些城市的纬度和经度。我想获取每个城市的温度并在我的应用程序中显示它们。如果有更好的主意,我会很高兴知道什么?
    • 如果您想一次获取所有城市的温度,并且考虑到只有几个城市的情况,那么您可以继续在 for 循环中调用 Fetch。但是考虑到城市多的场景,在for循环中调用api会降低性能。在这种情况下,我建议使用一个 API,您可以通过一次 Fetch 调用获取所有城市的天气更新。因为在您的应用程序中使用 json 比在 for 循环中调用 API 成本更低。
    • 不,有很多城市。我想在 FlatList 中显示所有城市。但我希望每个城市的温度显示给用户。
    • 在这种情况下,您最好寻找一个 api,您可以在一次 api 调用中获取所有城市的天气信息。在单个 api 调用中获取它们,存储在变量中并根据需要显示。否则,如果您尝试在循环中调用每一个,则会消耗大量带宽并降低性能。
    猜你喜欢
    • 2022-12-18
    • 2019-05-28
    • 1970-01-01
    • 2021-03-08
    • 2020-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    相关资源
    最近更新 更多