问题描述
我正在尝试获取多个.png图像,然后相应地设置我的状态。
我设法不使用Promise.all异步获取所有url;但是,随后的setstate函数在promise解决之前就已执行。因此,我将代码更改为包括promise.all
,但似乎我没有正确使用该函数。我收到错误“未定义不是函数(在'... podexData.map ...'附近)”。有人知道发生了什么吗?
解决方法
执行以下操作
const reqArray = [];
podexData.map(pokemon => reqArray.push(fetch(pokemon.url)));
Promise.all(reqArray).then(allResponses => {
allResponses.map(pokemonData =>
dex.push(....);
);
this.setState({...});
...
})