Skip to content

Instantly share code, notes, and snippets.

@slmcmahon
Last active November 16, 2017 02:39
Show Gist options
  • Save slmcmahon/8722db85e58cd8c90e275a85391b37d2 to your computer and use it in GitHub Desktop.
Save slmcmahon/8722db85e58cd8c90e275a85391b37d2 to your computer and use it in GitHub Desktop.
Simple Example of Loading Data in React
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