I'm trying to display an element from my items array. I can log the element to the console... but can't get it render. In my setState, I made itemDetail that takes the first element from the items array: (perhaps there's a better way to do that?)
this.setState({ items: items, itemDetail: items[0], value: this.state.value })
When I try to call {itemDetail} it is giving me the error:
"Objects are not valid as a React child (found: object with keys {id, name})...."
Full Code below. How can I render the first element from my array in React? Or better yet, display an element by Id. Thanks.
constructor(props){
super(props);
this.state = {
items: []
}
this.handleChange = this.handleChange.bind(this);
}
handleChange(event) {
this.setState({ value: event.target.value });
}
componentDidMount(){
const items = [
{
'id': 0,
'name': 'firstName'
},
{
'id': 1,
'name': 'secondName'
}
];
this.setState({ items: items, itemDetail: items[0] });
}
render(){
const { items, itemDetail } = this.state;
console.log(itemDetail);
return(
<div className="container">
{itemDetail}
</div>
);
}