Skip to content

Instantly share code, notes, and snippets.

@pe3
Last active October 22, 2015 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pe3/073f902da983ae0f952e to your computer and use it in GitHub Desktop.
Save pe3/073f902da983ae0f952e to your computer and use it in GitHub Desktop.
REST call in componentWillMount
import React from 'react'
import ReactDOM from 'react-dom'
import fetch from 'isomorphic-fetch'
const App = () => (
<div>
<RepositoryList id="pe3"/>
</div>
)
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 reposForUser = (username) => {
const url = `https://api.github.com/users/${username}/repos`
return fetch(url).then(response => response.json())
}
ReactDOM.render(<App />, document.getElementById('content'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment