【问题标题】:Dictionary values in React props (Typescript)React 道具中的字典值(Typescript)
【发布时间】:2022-01-28 21:36:51
【问题描述】:

给定一个定义了 props 接口 ProductProps 的组件 Product:

interface ProductProps {
  name: string,
  price: {
    amount: number,
    currency: string
  }
}

如何在组件标签中为price 分配值,即我想要一些效果:

<Product name="Nice Shoes" price.amount={100} price.currency="$" />

【问题讨论】:

  • 只使用一个对象字面量。 &lt;Product name="Nice Shoes" price={{amount :100, currency:"$"}} /&gt;
  • 谢谢。当我尝试类似的方法并遗漏了一套牙套时,我放弃得太快了。

标签: javascript reactjs typescript next.js react-props


【解决方案1】:

因为价格也是一个对象,你可以这样传递:

<Product name="Nice Shoes" price={{amount :100, currency:"$"}} />

第一组括号用于 jsx 语法,第二组括号用于对象本身

【讨论】:

  • 谢谢。当我尝试类似的方法并遗漏了一套牙套时,我放弃得太快了。
  • 欢迎您,别担心我们所有人都会遇到 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-01
  • 2021-10-21
  • 1970-01-01
  • 2021-07-21
  • 2019-06-23
  • 2019-10-23
  • 2020-06-27
相关资源
最近更新 更多