0

Here is a code:

  render() {
    return (
        <div
            <div className="container-center">
                <div className="container-center-inner">

                    <div>
                        <div className="container-checkout">
                            <div>
                                <div className="container-cart">
                                {
                                    this.props.cartStore.items.forEach(function(item, i, arr) {


                                        return (
                                            <div className="basket-item" key={i}>
                                                <div>
                                                    <div className="basket-item-image">
                                                        <TShirt className="color-tshirt-small" width="130" height="176" images={item.cartItemTShirt.image} color={item.item.color} />
                                                    </div>

                                                </div>
                                            </div>
                                        )

                                    })
                                }
                                </div>

                            </div>
                        </div>
                    </div>
                </div>
            </div>

        </div>
    );
}

Container "container-cart" is empty, but this.props.cartStore.items has values, and should work. When I try to execute code before return of render, the all working fine, but when I place it into return, it returns nothing. I think something wrong in loop, but I dont know, any help?

1 Answer 1

1

Your first <div> in your return statement is missing a closing tag (>).

I'd recommend cutting a lot of the superfluous tags or compartmentalising your component hierarchy to make such debugging easier in the future.

Also, convention is to use map() rather than forEach() to return an array of components.

See below for a complete example.

// Render.
render() {
  return (
    <div>
      <div className="container-center">
        <div className="container-center-inner">
          <div>
            <div className="container-checkout">
              <div>
                <div className="container-cart">
                  {this.props.cartStore.items.map((item, i, arr) => {
                    return (
                      <BasketItem item={item} i={i}/>
                    )
                  })}
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  )
}

// Basket Item.
const BasketItem = ({item, i, arr}) => (
  <div className="basket-item" key={i}>
    <div>
      <div className="basket-item-image">
          <TShirt className="color-tshirt-small" width="130" height="176" images={item.cartItemTShirt.image} color={item.item.color} />
      </div>
    </div>
  </div>
)
Sign up to request clarification or add additional context in comments.

1 Comment

It also worth to mention that forEach does not return an array but map does, and this is what makes difference.

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.