我有
const [items,setItems] = useState<any[]>(itemsB.map(() => ({ loading: true })));
和
itemsB.map(async (itemB: any,index: number) => {
searchItems(itemB).then(result => {
const newItems = [...items]; // Ref A
newItems[index] = result;
setItems(newItems);
})
})
因为内部函数是异步提取,所以项目以不可预测的顺序返回,我想在它们准备好后立即将它们添加到items
中,以实际结果更改占位符加载对象。几乎可以做到这一点,但是在Ref A处引用的items
不会更新,因此它会循环更改所有内容,最后仅显示要检索的最后一个项目。
如果我执行Promise.all().then()
,它会等到检索到所有项目以执行then
部分,因此我想知道如何在解决项目时设置它们。