Composing components

We've talked a lot about composability in the way of creating components by composing React's default components. However, we haven't showed how to compose custom components into bigger components.

As you might have guessed, this should be a pretty simple exercise, and to demonstrate how this works, we are going to implement a component to list investments, as follows:

var InvestmentList = React.createClass({
  render: function () {
    var onClickDelete = this.props.onClickDelete;

    var listItems = this.props.investments.map(function (investment) {
      return <InvestmentListItem investment={investment}
                  onClickDelete={onClickDelete.bind(null, investment)}/>;
    });

    return <ul className="investment-list">{listItems}</ul>;
  }
});

It is as simple as using the already available InvestmentListItem global variable as the root element of the InvestmentList component.

The component expects an investments prop to be an array of investments. It then maps it through creating an InvestmentListItem element for each investment in the array.

Finally, it uses the listItems array as the children of an ul element, effectively defining how to render the list of investments.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset