简体   繁体   English

reactjs - 在下次提取时使用提取的数据 (axios)

[英]reactjs - use fetched data in next fetch (axios)

How can I use data from my first fetch in the second fetch?如何在第二次提取中使用第一次提取的数据? I am getting undefined for the first search and then it's ok, how can I get it on the first search without getting "undefined"?我在第一次搜索时得到 undefined 然后没关系,我怎样才能在第一次搜索时得到它而不得到“undefined”?

  const [summoner, setSummoner] = useState("");
  const [playerData, setPlayerData] = useState({});
  const [playerRank, setPlayerRank] = useState({});

  function searchPlayer(event) {
    axios.get("API1" + summoner + API_KEY).then(function (response) {
      setPlayerData(response.data);
    }).catch(function (error) {
      console.log(error);
    });

    console.log(playerData.id);

    axios.get("API2" + playerData.id + API_KEY).then(function (response) {
      setPlayerRank(response.data)
    }).catch(function (error) {
      console.log(error);
    });
  }

This is my console:这是我的控制台:

log: undefined
log: id

I believe in React JavaScript, Axios sends a call out to the server and then continues, so the console.log function is called before the server response has been received and the callback runs.我相信 React JavaScript、Axios 向服务器发出调用然后继续,因此在收到服务器响应并运行回调之前调用 console.log function。 You either need wait for the response with the await operator or the better solution is to place your second API call in the call back function. Then it will have the needed data for the second API call.您要么需要等待操作员的响应,要么更好的解决方案是将您的第二个 API 调用置于回调 function 中。然后它将获得第二个 API 调用所需的数据。

If you are working on the API as well, you might look at getting everything from the server in one API call.如果您也在处理 API,您可能会考虑在一次 API 调用中从服务器获取所有内容。 This will involve working on the server side and I did not know from your post if you have access to the API end point coding.这将涉及在服务器端工作,如果您有权访问 API 端点编码,我不知道您的帖子。 I hope this helps or gets you looking in the right direction.我希望这有助于或让您朝着正确的方向看。

I recommend you to use something like useEffect to handle fetching data in react.我建议你使用类似useEffect的东西来处理在反应中获取数据。 Not sure what event is used for, but looked unused so if that's the case you can do the following:不确定用于什么event ,但看起来未使用,所以如果是这种情况,您可以执行以下操作:

  const [summoner, setSummoner] = useState("");
  const [playerData, setPlayerData] = useState({});
  const [playerRank, setPlayerRank] = useState({});

  function getPlayerData() {
    axios.get("API1" + summoner + API_KEY).then(function (response) {
      setPlayerData(response.data);
    }).catch(function (error) {
      console.log(error);
    });
  }

  function getPlayerRank() {
    if(!playerData.id) return;
    axios.get("API2" + playerData.id + API_KEY).then(function (response) {
      setPlayerRank(response.data)
    }).catch(function (error) {
      console.log(error);
    });
  }

  useEffect(() => {
    getPlayerData()
  }, [])

  useEffect(() => {
    getPlayerRank()
  }, [playerData])

This setup will trigger the getPlayerRank function any time playerData changes.此设置将在playerData更改时触发getPlayerRank function。 If event is used in either request, you want to pass that into the dependency array as well so you would end up with如果event在任一请求中使用,您也想将其传递到依赖项数组中,这样您最终会得到

  useEffect(() => {
    getPlayerData(event)
  }, [event])

  useEffect(() => {
    getPlayerRank()
  }, [playerData])

https://reactjs.org/docs/hooks-effect.html https://reactjs.org/docs/hooks-effect.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM