Skip to content

Instantly share code, notes, and snippets.

@hutchcrowley
Last active December 12, 2019 19:16
Show Gist options
  • Save hutchcrowley/b6ff3bbd6ad1136da33b0238cfd4f85c to your computer and use it in GitHub Desktop.
Save hutchcrowley/b6ff3bbd6ad1136da33b0238cfd4f85c to your computer and use it in GitHub Desktop.
Attempting to retrieve user data from the GitHub API and render the data using React class component lifecycle methods. Help!
import React from 'react'
import './App.css'
import axios from 'axios'
import UserCard from './Components/UserCard'
// import FollowersList from './Components/FollowersList'
class App extends React.Component {
constructor(props) {
super(props)
this.state = {
newUser: [],
userInfo: function getUserInfo() {
return axios({
method: 'get',
url: `https://api.github.com/users/hutchcrowley`,
headers: {
SECRET AUTHORIZATION TOKEN
}
})
},
followerInfo: function getFollowerInfo() {
return axios({
method: 'get',
url: `https://api.github.com/users/hutchcrowley/followers`,
headers: {
Authorization: 'token 32f7bb8cac2e59246e7acc27984bce12b97c9b34'
}
})
}
}
console.log('Application state data inside of constructor: ', this.state)
}
componentDidMount() {
// Fetch initial data from API, then set state to the new data
console.log('Current Application State Post API CALL: ', this.state)
axios
.all([this.state.userInfo(), this.state.followerInfo()])
.then(
axios.spread((res, fRes) => {
this.setState({
newUsers: res.data,
newFollowers: fRes.data,
newUser: Object.entries(this.newUsers)
})
})
)
.catch(err => console.log('Error: data not returned from server', err))
}
render() {
console.log('newUsers object on state directly after Render Method Call: ', this.state.newUsers)
// Render JSX to the DOM
return (
<section className='App'>
<header className='App-header'>
<h1>Hutch Crowley's Github Info</h1>
</header>
<div className='user-card'>
{this.state.newUser.map(users => (
<UserCard
key={users.id}
userPicture={users.gravar_url}
name={users.name}
location={users.location}
bio={users.bio}
profile={users.htm_url}
hireable={true}
followers={users.followers_url}
gists={users.gists}
repos={users.repos}
publicRepos={users.repos_url}
/>
))}
console.log('Application State in .map method: ', this.values)
</div>
</section>
)
}
}
export default App

Help!

I've been learning React, but this is my first exposure to trying to honestly figure out class components/ lifecycle methods. I thought they looked easier for some reason, but I'm so used to functional components with hooks now that I'm totally lost on where I should be adding variables, functions, etc. Basically, the main problem that I am having is that newUser.data that is returned from the API is coming in as an object, and not an array as I expected. Normally, this wouldn't be a problem, however I can't seem to get it to work. I did not realize how finicky class components can be about where to place things. IF anyone has any suggestions I'd really appreciate it! Cheers.

{
"login": "hutchcrowley",
"id": 16690781,
"node_id": "MDQ6VXNlcjE2NjkwNzgx",
"avatar_url": "https://avatars3.githubusercontent.com/u/16690781?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/hutchcrowley",
"html_url": "https://github.com/hutchcrowley",
"followers_url": "https://api.github.com/users/hutchcrowley/followers",
"following_url": "https://api.github.com/users/hutchcrowley/following{/other_user}",
"gists_url": "https://api.github.com/users/hutchcrowley/gists{/gist_id}",
"starred_url": "https://api.github.com/users/hutchcrowley/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/hutchcrowley/subscriptions",
"organizations_url": "https://api.github.com/users/hutchcrowley/orgs",
"repos_url": "https://api.github.com/users/hutchcrowley/repos",
"events_url": "https://api.github.com/users/hutchcrowley/events{/privacy}",
"received_events_url": "https://api.github.com/users/hutchcrowley/received_events",
"type": "User",
"site_admin": false,
"name": "Hutch Crowley",
"company": "Lambda School",
"blog": "https://www.hutchssite.wordpress.com",
"location": "Nashville, TN",
"email": null,
"hireable": true,
"bio": "From Nashville, TN. Spent some years hopping between the west coast and my hometown. I love music and coding.",
"public_repos": 44,
"public_gists": 2,
"followers": 14,
"following": 24,
"created_at": "2016-01-13T18:56:33Z",
"updated_at": "2019-12-12T09:52:32Z"
}
import React from 'react'
const UserCard = props => {
console.log(props.users.id)
return(
<div className='user'>
<h1>
key: {props.users.id}
</h1>
</div>
)
}
export default UserCard
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment