【发布时间】:2019-09-09 15:12:12
【问题描述】:
我想了解如何在 js 中正确使用 ... 运算符,尤其是 js 的 map 函数。
假设我们有以下数据:
const SHOP_DATA = [
{
id: 1,
title: 'Hats',
routeName: 'hats',
items: [
{
id: 1,
name: 'Brown Brim',
imageUrl: 'https://i.ibb.co/ZYW3VTp/brown-brim.png',
price: 25
}]
}
]
现在我想将值作为道具发送到另一个组件:
SHOP_DATA.map(({ id, ...otherProps }) => (
<CollectionPreview key={id} {...otherProps} />
如果我理解正确,第一个 ... 是一个休息运算符,这意味着我们将值汇集到一个新数组中。 第二个 ... 是扩展运算符,这意味着我们正在从数组中扩展这些值
然后我们需要在 ColectionPreview 中再次使用 ... 运算符将它们展开。
const CollectionPreview = ({ id, ...otherProps }) => {
不懂数据流,为什么我们刚才做的就相当于做:
SHOP_DATA.map(({ id, title , items }) => (
<CollectionPreview key={id} title = {title} items = {items} />
【问题讨论】:
标签: reactjs rest dictionary spread