1

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="$" />
2
  • 2
    Just use an object literal. <Product name="Nice Shoes" price={{amount :100, currency:"$"}} /> Commented Jan 28, 2022 at 13:21
  • Thanks. Gave up too quickly when I tried something similar and left out one set of braces. Commented Jan 28, 2022 at 13:27

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Gave up too quickly when I tried something similar and left out one set of braces.
Your welcome, don't worry happens to all of us :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.