Created
February 7, 2023 12:53
-
-
Save wpflames/78b069e6bf416601eaa5abfaf41aac49 to your computer and use it in GitHub Desktop.
React HTTP Request with Axios
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, { Fragment, Component } from 'react'; | |
import Navbar from './components/layout/Navbar'; | |
import Users from './components/users/Users'; | |
import axios from 'axios'; | |
import './App.css'; | |
class App extends Component { | |
state = { | |
users: [], | |
loading: false, | |
}; | |
async componentDidMount() { | |
this.setState({ loading: true }); | |
const res = await axios.get('https://api.github.com/users'); | |
this.setState({ users: res.data, loading: false }); | |
} | |
render() { | |
return ( | |
<Fragment> | |
<Navbar /> | |
<div className='container'> | |
<Users loading={this.state.loading} users={this.state.users} /> | |
</div> | |
</Fragment> | |
); | |
} | |
} | |
export default App; |
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 PropTypes from 'prop-types'; | |
const UserItem = ({ user: { login, avatar_url, html_url } }) => { | |
return ( | |
<div className='card text-center'> | |
<img | |
className='round-img' | |
src={avatar_url} | |
alt={login} | |
style={{ width: '60px' }} | |
/> | |
<h3 className='card-title text-center'>{login}</h3> | |
<a className='btn btn-dark btn-sm my-1' href={html_url}> | |
More | |
</a> | |
</div> | |
); | |
}; | |
UserItem.propTypes = { | |
user: PropTypes.object.isRequired, | |
}; | |
export default UserItem; |
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, { Component } from 'react'; | |
import UserItem from './UserItem'; | |
class Users extends Component { | |
render() { | |
return ( | |
<div style={userStyle}> | |
{this.props.users.map((user) => ( | |
<UserItem key={user.id} user={user} /> | |
))} | |
</div> | |
); | |
} | |
} | |
const userStyle = { | |
display: 'grid', | |
gridTemplateColumns: 'repeat(3, 1fr)', | |
gridGap: '1rem', | |
}; | |
export default Users; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment