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...