The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

Building a React.js App: Making Server Requests in React with Axios

Building a React.js App: Making Server Requests in React with Axios

9:30
In this lesson, we’ll walk through implementing Axios into our application in order to communicate with the Github API.
Watch this lesson now
Avatar
egghead.io

In this lesson, we’ll walk through implementing Axios into our application in order to communicate with the Github API.

Avatar
Daniel

Shouldn't the getGithubInfo() call be in componentWillReceiveProps as well? else the request to github API and update of the user info doesn't update until a page refresh.

Should it be within componentWillReceiveProps? Or am I missing something?

In reply to egghead.io
Avatar
Philip

Hi, I am getting an error. helpers.getGithubInfo is not a function? I have gone over the code like for like I am certain, pretty weird :)

In reply to egghead.io
Avatar
Tyler

Put your code up on Github and send me the URL. I'll take a look when I get a second.

In reply to Philip
Avatar
Philip

Thank you for the offer Tyler. I have it working now. Just did a copy/paste :(
Pretty sure the code was like for like, haha.
One of the many wonders of programming I guess :)

In reply to Tyler
Avatar
Tyler

You're correct. Now sure why I missed that.

In reply to Daniel
Avatar
Richard

Just working with the data returned from axios, and noticing issues when passing around objects in React. Is there any reference to this issue somewhere?

I am just trying to display a single property of the bio object returned from github, I can view it in the React console, however, not the on the html page. Any ideas?

So the repos display (its an array), but the bio does not display, and I notice a console error complaining about working with the object.

Avatar
Tyler

Hi Richard,

What's the exact error message you're getting in the console?

In reply to Richard
Avatar
Manny

Hey Tyler,

I'm receiving the following error after searching for a github user:

Uncaught Error: Invariant Violation: Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons. Check the render method of UserProfile.

Below is a link to the repo for my code:

https://github.com/yooomanny/github-app

Avatar
Joel

Hey Tyler,

I'm receiving the following error after searching for a github user:

Uncaught Error: Invariant Violation: Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons. Check the render method of UserProfile.

Below is a link to the repo for my code:

https://github.com/yooomanny/github-app

Repos.js is likely cause of error. React expects the array to be an array or elements versus objects.

render: function() {
+ const repos = this.props.repos.map(repo => <div>{repo.name}</div>)
render (
In reply to Manny
Avatar
Manny

Thanks Joel.

I'm a bit confused with your code. Am I supposed to add the green line after line 8 in my repo.js file? If so, I just gave it a shot and received the same error.

By the way, I had the same error when I had " bio: {} " in my profile.js file, but it went away after setting it to an array and requiring it as an array for the mean time. How can I allow for objects in these instances?

In reply to Joel
Avatar
Joel

Thanks Joel.

I'm a bit confused with your code. Am I supposed to add the green line after line 8 in my repo.js file? If so, I just gave it a shot and received the same error.

By the way, I had the same error when I had " bio: {} " in my profile.js file, but it went away after setting it to an array and requiring it as an array for the mean time. How can I allow for objects in these instances?

I'm just guessing. You have to add JSX elements as children, not JavaScript objects/arrays of objects. I'm not actually running and debugging your repo.

In reply to Manny
Avatar
Manny

Gotcha, thanks.

In reply to Joel
Avatar
egghead.io

The lesson video has been updated!

Avatar
Alan

Thanks for the video! Question -- how were we able to make an API request to Github's API without generating an access token? Does axios somehow bypass that?

Avatar
Tyler

Hi Alan,

Github allows you to make a certain number of requests without an API key. Once you pass that amount (which is quickly if anyone is actually using your app), then you get rate-limited.

In reply to Alan
Avatar
Alan

Gotcha, that makes sense. I hit my limit -- it was easy to do when I'm using a hot loader!

On an unrelated note, when I try to make a call to http://www.bayareabikeshare.com/stations/json, I get an Access-Control-Allow-Origin error. I'm guessing this has to do with some cross domain issue. Why were we able to make an API call to Github but not a call to any JSON endpoint?

In reply to Tyler
Avatar
Tyler

Another great observation. The Github... "API supports Cross Origin Resource Sharing (CORS) for AJAX requests from any origin." (https://developer.github.com/v3/) Where as bayareabikeshare obviously does not.

In reply to Alan
Avatar
Divyendu

Hey Tyler,
Good video, continuing to talk about

https://github.com/tylermcginnis/github-notetaker-egghead/blob/09-axios/app/utils/helpers.js

What if the promises are interdependent. In this case, the both calls required username as parameter, but how can we use Axios.all when we have two service calls A, B where parameter of B is retrieved from service call A

Avatar
Tyler

Hi Divyendu,

In that case you'd chain your promises rather than using .all. I found this tutorial that does a quick job of explaining how you'd do that - https://html5hive.org/how-to-chain-javascript-promises/.

In reply to Divyendu
HEY, QUICK QUESTION!
Joel's Head
Why are we asking?