【发布时间】:2021-05-01 06:42:59
【问题描述】:
我有这个代码:
let [totalPromos, setTotalPromos] = useState(0);
for (let item of cart.items){
let response = await api.get('/products/' + item.id);
let price = response.data.price;
let promotion = response.data.promotions[0];
setTotalPromos(totalPromos + 1);
}
我在购物车中有 2 件商品,我用 axios 调用了一个 api 以实现其促销。我无法总结我产品的促销总数。该值仅获得此代码中的最终值,即 1。我做错了什么?
【问题讨论】:
-
这能回答你的问题吗? try/catch blocks with async/await
标签: reactjs react-native ecmascript-6 react-hooks ecmascript-2016