[英]Objects are not valid as a React child. If you meant to render a collection of children, use an array instead - Error Solidity - React
[英]How to fix error: Objects are not valid as a React child. If you meant to render a collection of children, use an array instead
我正在嘗試使用一組對象設置 a 狀態,但出現上述錯誤,我不知道如何解決它。
我的代碼:
const [itemsInCart, setItemsInCart] = useState([]);
useEffect(() => {
const fetchData = async () => {
await fetch("https://fakestoreapi.com/products?limit=16")
.then((res) => res.json())
.then((data) => {
const initialItemCount = data.map((item) => {
return {
name: item.title,
qty: 0,
};
});
setItemsInCart(initialItemCount);
});
};
fetchData();
}, []);
我嘗試console.log(Array.isArray(initialItemCount))
來檢查它是否是一個數組並且它返回了 true。 那么為什么說使用數組呢? 謝謝
您可以使用斷點查看itemsInCart
的最終值,以檢查此數組是否包含 React 子組件的列表。
從您的代碼中,我猜該數組僅包含 {name and qty} 的對象,無法直接呈現。
您可以嘗試將這些對象解析為組件以呈現 UI。
希望這有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.