I am working on my first experiment with React, trying to consume and display some data via an API that I also wrote (using the Django REST framework)
I am seeing the objects populate in the Console, but they don't render on the page. I am also seeing an error in the console:
Uncaught (in promise) TypeError: Cannot read property 'length' of undefined
Which refers to line #18 in my ChangeList component:
import React from 'react';
class ChangeList extends React.Component {
constructor() {
super();
this.state={items:[]};
}
componentDidMount(){
fetch(`http://127.0.0.1:8000/backup/changes/1/Lead.json`)
.then(result=>result.json())
.then(items=>console.log(items) )
.then(items=>this.setState( {items} ))
}
render() {
return(
<ul>
{this.state.items.length ?
this.state.items.map(item => <li> {item} </li>)
: <li>Loading...</li>
}
</ul>
)
}
}
export default ChangeList
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
So how do I restructure this React component so that this.state.items.length is evaluated after all of the items are loaded? Or am I still misunderstanding the issue, and if so, what am I doing wrong in rendering the data?