Skip to content

Instantly share code, notes, and snippets.

@maecapozzi
Created July 30, 2017 21:20
Show Gist options
  • Save maecapozzi/1ebdf30113b07a21cf375e0d91c70d80 to your computer and use it in GitHub Desktop.
Save maecapozzi/1ebdf30113b07a21cf375e0d91c70d80 to your computer and use it in GitHub Desktop.
Example of making a HTTP request in React
import React from 'react'
import axios from 'axios'
import UserProfile from './UserProfile'
class UserProfileContainer extends React.Component {
state = {}
componentDidMount() {
axios.get('http://localhost:3001/users', {
params: {
id: 1
}
})
.then(response => {
const user = response.data[0]
this.setState({id: user.id, name: user.name })
})
.catch(error => {
console.log(error)
})
}
render() {
return <UserProfile name={this.state.name}/>
}
}
export default UserProfileContainer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment