【发布时间】:2019-04-12 16:24:45
【问题描述】:
我正在尝试呈现购物车中的商品列表。项目列表作为道具从我的父组件传递。我正在调用 mapCartItemsToItems 但它没有呈现并表明
“对象作为 React 子对象无效(发现:对象的键为 {childKey, header, meta, extra})。如果您要渲染一组子对象,请改用数组。”
const mapCartItemsToItems = items =>
items.map(({ id, product_id, name, quantity, meta }) => {
const price = meta.display_price.with_tax.unit.formatted || null
return {
childKey: id,
header: (
<Link href={`/product?id=${product_id}`} passHref>
<div>{name}</div>
</Link>
),
meta: `${quantity}x ${price}`,
extra: <button onClick={() => removeFromCart(id)}>Remove</button>
}
})
return <div>{mapCartItemsToItems(items)}</div>
【问题讨论】:
-
return {你要返回一个对象? -
另外,它应该返回 JSX,而不是 javascript 对象。
标签: javascript arrays reactjs object next.js