链请求所以我可以在第二个请求中使用第一个请求中的数据

问题描述 投票:1回答:1

我刚刚开始了一个简单的天气应用程序项目来训练React和数据获取。

import React, { Component } from 'react';
import './App.css';

import axios from 'axios';


class App extends Component {

  constructor(props) {
    super(props);

    this.state = {
      city: "",
      id: 0
    }

    this.choseCity = this.choseCity.bind(this);
    this.cityName = this.cityName.bind(this);
    this.cityInfo = this.cityInfo.bind(this);
  }


  chooseCity(e) {
    console.log(e.target.value)
    this.setState({
      city: e.target.value
    });
  }


  cityName() {
    axios.get(`https://www.metaweather.com/api/location/search/?query=${this.state.city}`)
      .then(res => res.data)
      .then(data => this.setState({
        city: data[0].title,
        id: data[0].woeid}))
  }

  cityInfo() {
    axios.get(`https://www.metaweather.com/api/location/${this.state.id}/`)
      .then(res => console.log(res.data))
  }


  render() {
    return (
      <div className="App">
        <input type="text" placeholder="Enter city name" value={this.state.city} onChange={this.chooseCity}/>
        <button onClick={this.cityName}>Name</button>
        <button onClick={this.cityInfo}>Info</button>

      </div>
    );
  }
}

export default App;

所以,我有2个函数(cityName和cityInfo),我可以在2个不同的onClick事件上执行。他们似乎都独立工作。

  • cityName()请求存储在我的状态中的数据。
  • cityInfo()在请求的url中使用此状态来获取进一步的信息。

我试图链接它们以便能够在一次调用中检索所有数据,但由于它们都是异步的,我的第二个请求在第一个数据存储在我的状态之前开始,并且没有办法进入api我可以在一个请求中直接获取我需要的信息。

我尝试了一些事情,比如将它们分组到一个函数中,但到目前为止还没有结论。

解决方案:@elsyr

这是如何在一个函数中链接,使用请求之间的数据:

cityInfo() {
    axios.get(`https://www.metaweather.com/api/location/search/?query=${this.state.city}`)
      .then(res => res.data)
      .then(data => {
        axios.get('https://www.metaweather.com/api/location/' + data[0].woeid)
          .then(res => res.data)
          .then (data => this.setState({
            info: data
          }))
      });
  }
javascript reactjs httprequest axios
1个回答
1
投票

据我所知,你想在cityInfo中设置状态之后立即触发cityName中的调用。

这是完全可能的 - setState是异步的(因为你似乎已经想到了),但是setState也有一个你可以使用的可选callback parameter。保证在修改状态后触发回调。

您的代码在cityName中看起来像这样:

cityName() {
    axios.get(`https://www.metaweather.com/api/location/search/?query=${this.state.city}`)
      .then(res => res.data)
      .then(data => this.setState({
        city: data[0].title,
        id: data[0].woeid}),
        this.cityInfo); // callback here! runs after the state is set!
 }

你有另一个选择就是将你的axios电话连在一起。如果我们在这里查看您的代码块:

cityName() {
    axios.get(`https://www.metaweather.com/api/location/search/?query=${this.state.city}`)
      .then(res => res.data)
      .then(data => {
          // data here already contains the id - data[0].woeid
          // we can just use it here to kick off another request - something like:
          // axios.get('https://www.metaweather.com/api/location/' + data[0].woeid)
      });
  }

因为你使用了.then(),你知道在开始你的cityInfo电话之前你肯定会得到你的数据。这可能需要更多的重构,但如果你只是保持cityName的状态在cityInfo中使用它,这可能是一个更好的主意。

© www.soinside.com 2019 - 2024. All rights reserved.