inspired by simple counter app implementations
Last active
September 15, 2017 12:01
-
-
Save pe3/8b3ff523717a461db73d2ab131f93578 to your computer and use it in GitHub Desktop.
Simple fetch based API client implementations
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 from 'react' | |
import ReactDOM from 'react-dom' | |
import fetch from 'isomorphic-fetch' | |
const reposUrl = username => `https://api.github.com/users/${username}/repos` | |
const reposForUser = (username) => { | |
return fetch(reposUrl(username)).then(response => response.json()) | |
} | |
class RepositoryList extends React.Component { | |
constructor(props) { | |
super(props) | |
this.state = { repos: [] } | |
} | |
componentWillMount() { | |
reposForUser(this.props.id) | |
.then((repos) => { | |
this.setState({repos: repos}) | |
}) | |
.catch((error) => { | |
console.log(error) | |
}) | |
} | |
render() { | |
return <pre>{JSON.stringify(this.state.repos, null, 2)}</pre> | |
} | |
} | |
const App = () => ( | |
<div> | |
<RepositoryList id="pe3"/> | |
</div> | |
) | |
ReactDOM.render(<App />, document.querySelector('#app')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment