reactjs - React - 如何在多次 api 调用后等待 UI 更新?
问题描述
我是新手,需要一些帮助。
我创建了一个 Pokedex,该应用程序通过 API 调用生成数据,但随后它需要执行另一个 API 调用以获取更多数据,然后以相同的方式处理更多数据。
在我的情况下,用户需要按三次按钮才能生成“完整”信息。
我是否需要使用 React 中实现的“useEffect”钩子来解决这个问题,如果需要,如何解决?
当前代码:
import Axios from "axios";
import React, { useState } from "react";
function PK() {
const api = Axios.create({
baseURL: "https://pokeapi.co/api/v2/",
});
const [pokemon, setPokemon] = useState({});
const [pokemonDescription, fetchDescription] = useState({});
const [evolution, pokemonEvolution] = useState({});
const searchPokemon = () => {
api.get(`pokemon/magnemite`).then((response) => {
setPokemon({
name: response.data.name,
height: response.data.height,
weight: response.data.weight,
img: response.data.sprites.front_default,
id: response.data.id,
type: response.data.types[0].type.name,
type2: response.data.types[1].type.name,
});
api.get(`pokemon-species/${pokemon.id}/`).then((response) => {
console.log(response.data.evolution_chain);
fetchDescription({
entry: response.data.flavor_text_entries[0].flavor_text,
evolution: response.data.evolution_chain.url,
});
api.get(`${pokemonDescription.evolution}`).then((response) => {
console.log(response.data.chain.evolves_to[0].species.name);
pokemonEvolution({
evolution: response.data.chain.evolves_to[0].species.name,
});
});
});
});
};
return (
<div>
<h1 style={{ textTransform: "capitalize" }}>{pokemon.name}</h1>
<h1>#{pokemon.id}</h1>
<h1>{pokemon.weight / 10} Kg</h1>
<h1>{pokemon.height * 10} Cm</h1>
<img src={pokemon.img} alt="" />
<h1 style={{ textTransform: "capitalize" }}>
Type: {pokemon.type} {pokemon.type2}
</h1>
<h2 style={{ textTransform: "capitalize" }}>
{pokemonDescription.entry}
</h2>
<h1 style={{ textTransform: "capitalize" }}>
Evolution: {evolution.evolution}
</h1>
<button onClick={searchPokemon}>Click me</button>
</div>
);
}
export default PK;
所以,我不知道如何创建一个好的解决方案来同时进行调用或在所有调用完成后更新 UI。
提前致谢。
解决方案
const searchPokemon = () => {
api.get(`pokemon/magnemite`).then((response) => {
setPokemon({
name: response.data.name,
height: response.data.height,
weight: response.data.weight,
img: response.data.sprites.front_default,
id: response.data.id,
type: response.data.types[0].type.name,
type2: response.data.types[1].type.name
});
api.get(`pokemon-species/${response.data.id}/`).then((response) => {
fetchDescription({
entry: response.data.flavor_text_entries[0].flavor_text,
evolution: response.data.evolution_chain.url
});
api.get(`${response.data.evolution_chain.url}`).then((response) => {
pokemonEvolution({
evolution: response.data.chain.evolves_to[0].species.name
});
});
});
});
};
用这个替换你的搜索功能,它会运行得很好。
推荐阅读
- javascript - 如何使用 JavaScript 验证表单输入
- javascript - HTML5 中的旧脚本
- flutter - Flutter驱动程序如何等待特定元素具有特定文本
- python - 'mysql.connector.django' 不是可用的数据库后端 Django 版本 3.+
- timer - RxJs:重复检查全局变量的值,直到值满足要求或达到最大时间限制
- java - 什么是
预期的Java错误是什么意思? - javascript - 使用 Electron Builder 编译后,Electron 主进程不会加载其他文件
- odoo - 如何在 Odoo 中显示带有来自控制器的警告消息的对话框?
- amazon-web-services - 如何使用一个位置路径从多个文件夹创建多个表,而雅典娜也应该使用胶水爬虫处理它
- spring-data-jpa - 如何在 Spring Data jpa 中获取 Connection 对象?