0

I have this long-winded component which is passing many props. Is there a dryer way of passing this component to child?

Thanks.

let products;
if (!this.state.loading) {
  products = this.state.products.map(product => (
    <Product
      description={product.description}
      title={product.title}
      originalRetailPrice={product.original_retail_price.formatted_value}
      retailPrice={product.retail_price.formatted_value}
      priceValue={product.retail_price.value}
      discounted={product.discount}
      imageURL={product.cover_image_url}
      key={product.uuid}
      uuid={product.uuid}
      onClick={this.addToBagHandler}
    />
  ));
}

These are properties returned from an api call.

1
  • You could parse the the data when it's returned and consolidate it into a single object called product or something similar. Looks like you're already getting an object like that. Commented Dec 4, 2019 at 14:50

2 Answers 2

2

You just pass the whole product instead

let products;
if (!this.state.loading) {
  products = this.state.products.map(product => (
    <Product
      product={product}
      onClick={this.addToBagHandler}
    />
  ));
}

And inside Product you can access them like this:

const { description, title, discount } = this.props.product
Sign up to request clarification or add additional context in comments.

Comments

0

You can destructure like this

   let products;

   if (!this.state.loading) {
    products = this.state.products.map(({
    description,title, original_retail_price,
    retail_price, retail_price,discount,cover_image_url,
    uuid}) => 
    <Product 
    description={description}
    title={title}
    originalRetailPrice={original_retail_price.formatted_value}
    retailPrice={retail_price.formatted_value}
    priceValue={retail_price.value}
    discounted={discount} 
    imageURL={cover_image_url}
    key={uuid}
    uuid={uuid}
    onClick={this.addToBagHandler}/>
  )}

Comments

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.