Skip to content

Instantly share code, notes, and snippets.

@aerrity
Last active February 17, 2018 22:54
Show Gist options
  • Save aerrity/f105bd5456606f185e0340a8c44e61a4 to your computer and use it in GitHub Desktop.
Save aerrity/f105bd5456606f185e0340a8c44e61a4 to your computer and use it in GitHub Desktop.
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