Im trying to create a small React.js application, and my component structure looks like this:
MainComponent - CategoryList -Category - ItemsList -Item
My MainContent component makes an ajax request for its state data in componentDidRender: which this object returns:
data:[ Object[0] -name -items[] , Object[1], Object[2] ]
Now I want my category list to list all categories by name, which works fine, but I also want to print the elements of the selected category. This is my ItemsList component:
var ItemsList = React.createClass({ render:function(){ var itemNodes = this.props.category.items.map(function(item){ return ( <Item name={item.name} /> ); }); return( <div className="itemList"> {itemNodes} </div> ); } });
And this is how I pass the "category" property from my parent component
<ItemsList category={this.state.data[0]} />
I get an error: "Can't read properties items of undefined" means that categorical support has never been assigned. I know that this.state.data contains an array of objects, so I don't see an error here.
What am I doing wrong?
EDIT: on request, this is my main component:
var MainComponent = React.createClass({ getInitialState:function(){ return {data: []}; }, componentDidMount:function(){ $.ajax({ type:'get', url: '/categories', dataType: 'json', success:function(data){ this.setState({data: data}); }.bind(this) }); }, render: function(){ return ( <div className="row"> <div className="col-md-6"> <CategoryList categories={this.state.data} /> </div> <div className="col-md-6"> <ItemsList category={this.state.data[0]} /> </div> </div> ); } });