Created
July 30, 2017 21:20
-
-
Save maecapozzi/1ebdf30113b07a21cf375e0d91c70d80 to your computer and use it in GitHub Desktop.
Example of making a HTTP request in React
This file contains 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 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