Skip to content

Instantly share code, notes, and snippets.

@moonformeli
Created May 18, 2017 05:10
Show Gist options
  • Save moonformeli/d025e4b08dcedfb745164abc6066b956 to your computer and use it in GitHub Desktop.
Save moonformeli/d025e4b08dcedfb745164abc6066b956 to your computer and use it in GitHub Desktop.
ComponentWillMount example 2
class Quiz extends Component {
// Added this:
constructor(props) {
super(props);
// Assign state itself, and a default value for items
this.state = {
items: []
};
}
componentWillMount() {
axios.get('/thedata').then(res => {
this.setState({items: res.data});
});
}
render() {
return (
<ul>
{this.state.items.map(item =>
<li key={item.id}>{item.name}</li>
)}
</ul>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment