Home > Software design >  Dictionary values in React props (Typescript)
Dictionary values in React props (Typescript)

Time:01-28

Given a component Product with a props interface ProductProps defined:

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

how do I assign the values for price in the component tag i.e. I want something to the effect of:

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

CodePudding user response:

Because price is an object too you could pass like so:

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

The first set of parenthesis are for the jsx syntax and the second is for the object itself

  •  Tags:  
  • Related