Created
May 18, 2017 05:10
-
-
Save moonformeli/d025e4b08dcedfb745164abc6066b956 to your computer and use it in GitHub Desktop.
ComponentWillMount example 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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