Last active
November 16, 2017 02:39
-
-
Save slmcmahon/8722db85e58cd8c90e275a85391b37d2 to your computer and use it in GitHub Desktop.
Simple Example of Loading Data in React
This file contains hidden or 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, { Component } from 'react' | |
export class UserList extends Component { | |
constructor() { | |
super(); | |
this.state = { | |
users: [], | |
} | |
}; | |
componentDidMount() { | |
fetch('https://randomuser.me/api?results=100').then(results => { | |
return results.json(); | |
}).then(data => { | |
var users = []; | |
data.results.forEach(u => { | |
users.push(u); | |
}); | |
this.setState({users: users}); | |
}); | |
} | |
render() { | |
return ( | |
<ul> | |
{this.state.users.map(function (u) { | |
return <li key='{u.login.md5}'>{u.name.last}, {u.name.first}</li> | |
})} | |
</ul> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment