Last active
February 17, 2018 22:54
-
-
Save aerrity/f105bd5456606f185e0340a8c44e61a4 to your computer and use it in GitHub Desktop.
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
import React from 'react'; | |
import ReactDOM from 'react-dom'; | |
class UserList extends React.Component { | |
constructor() { | |
super(); | |
this.state = {userName: ''}; | |
} | |
componentWillMount() { | |
fetch('https://randomuser.me/api') | |
.then(response => { | |
if(response.ok) return response.json(); | |
throw new Error('Request failed.'); | |
}) | |
.then(data => { | |
const name = data.results[0].name.first; | |
this.setState( {userName: name} ); | |
}) | |
.catch(error => { | |
console.log(error); | |
}); | |
} | |
render() { | |
return ( | |
<p>{this.state.userName}</p> | |
); | |
} | |
} | |
ReactDOM.render( | |
<UserList />, | |
document.getElementById('root') | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment