【发布时间】:2020-08-17 14:58:12
【问题描述】:
使用 Axios 抓取数据以构建 Pokedex 并基于不同的 API 调用,我想将来自两个或三个调用的数据合并到 on。拨打电话,我目前有:
const [species, setSpecies] = useState([]);
const [types, setTypes] = useState([]);
const [sprites, setSprites] = useState([]);
const [pokemon, setPokemon] = useState();
axios.get('https://pokeapi.co/api/v2/pokemon-species?limit=151')
.then(res => {
return axios.all(res.data.results.map(p => axios.get(p.url)));
})
.then(res => {
setSpecies(res.map(p => p.data));
return axios.all(res.map(s => axios.get(s.data.varieties[0].pokemon.url)));
}).then(res => {
setTypes(res.map(t => t.data.types));
setSprites(res.map(s => s.data.sprites.front_default));
});
我不知道是否有更简单/更好的方法,但我希望 setPokemon 将来自物种、类型和精灵的信息全部通过 id 合并。
提前感谢任何帮助/建议!
【问题讨论】:
标签: reactjs axios react-hooks