This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

React Native: Using fetch to load external API data

6:26 React lesson by

The way you make HTTP requests in React Native is with fetch. In this video, we'll add integration with the github API into our app using fetch.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

The way you make HTTP requests in React Native is with fetch. In this video, we'll add integration with the github API into our app using fetch.

Avatar
Sam

How would you go about appending a header to your request?

The approach that I took was: https://gist.github.com/samuelcouch/5d2b860a11f395f250a0

But all I get is an error that says "Can't find variable Headers" referencing line 4.

Avatar
Sam

...Preemptively posted this... Found the solution after looking in to Headers source. It just builds an object for you.

In reply to Sam
Avatar
valentin

When I link the api component to the main.js, I get a surprising message error: Requiring unknown module "../Utils/api". If you are sure the module is there, try restarting the packager. What does it mean to restart the packager ?

Avatar
Lars Jägare

In the video there is a sentence that fetch is not available in the browser yet. But there are polyfills if you want to use the same code in browser as in react-native, e.g. [[https://www.npmjs.com/package/isomorphic-fetch]]

In reply to egghead.io
Avatar
Tyler

Good advice. Ironically, Chrome started supporting fetch a few days after these videos came out too.

In reply to Lars Jägare
Avatar
Anders

I think the "back-tick templating thingy" is known as "interpolation"

Just a recap of where we're at. We have this handlesubmit function that's going to run whenever someone clicks on this button. What we want this handlesubmit button to do is it's going to show a spinner as the data is getting fetched. It's going to get that data and then pass that data to another component or another route, and that data will get rendered, and we'll see that route. So, the very first thing we're going to do is create this external API file.

Let's go to our app folder, create a new folder called utils, and in here, let's create a new file called api.js. All this file is going to be is it's going to be an object which has a few methods on it. Those methods are going to use fetch in order to get some information from GitHub.

Let's go ahead and export this. The very first method we're going to make is let's call it getrepos.

This method is going to accept the user name and we're going to format this username just a little bit, so let's do tolowercase on it as well as trim. Then, let's set up our URL.

Notice here I'm using back ticks, the reason for that is because back ticks allow us to do, or gives us the ability to use this ES6 template thing. I don't know exactly what you call that.

What this allows us to do is, if we throw in username here, this, addrepos to the end of it, this is the same thing as doing something like this, where you're concatenating this string. It's just an easier way and it looks a lot cleaner to do that.

I'm a huge fan of these back ticks in this templating language, or templating ability in ES6, so we're going to use it all over the place in this app.

We have our URL, and then what we'll do as we talked about, we'll use fetch and we're going to return that, so we'll get a promise returned, and then this fetch invocation is going to return us another promise, which will chain, and we'll get a response, and then we're going to return rows.json as we did before.

Just one thing that's new here is we're using this ES6 fat arrow, we'll get a few benefits from this. For one, we don't have to write function, it's less typing.

Another one, we don't have to worry, it doesn't really help us in this use case, if we were using this keyword inside of this function, what usually happens is every time you create a new function and that function gets invoked, that function has its own context. If you use fat arrow though, it will keep the context of the parent function, so you don't have to do like .bind or any of that crazy stuff.

But we don't really see that here, but we probably will in the future and I'll point that out when we get to it.

Now that we have this getrepos method, let's add another one called getbio. This is going to do a lot of the same thing, so I'm going to just copy this. We have our user name, we're not going to do slash repos, but we still have our URL, and we're going to return fetch at the URL, which returns us a promise that we can chain, and we'll get a response back, and then we'll return another promise.

All right, so there's our API, and it looks pretty good. Now, let's go back to our main.js file. Here's where we're going to want to invoke our getbio function.

Let's do, first, we need to require our API, so we're going to say var API = require, we're going to go into utils, and then our API. Now, we're going to do API.getrepos. This is expecting to receive our getbios, excuse me, it should be expecting to receive a user name, which we forgot.

Let's do getbio, we're going to pass in this.state.username. Remember we set up earlier this handlechange function that's updating the state whenever someone types into the input field.

So, when we invoke getbio that's going to return us a promise with this response, and so this response, ideally it's going to be something that we're getting from GitHub, and it's going to be basically an object with all of this username's property.

Then what we're going to do...If we try to look up a username that doesn't exist, then GitHub is going to populate this .message property on our response with "Not found."

If we find that, what we'll do is we'll set our state, and have our error be user not found, and then isloading will become false, but if the user was found, and we don't get an error, then what we'll do is we're going to use this.props.navigator.push. We're basically pushing a new route onto the stack.

The reason we're able to do this is because if you remember back in index.ios.js, we created this component. What we're going to do is we're going to push this new object, or we're basically just going to change routes.

The title of this is going to be the name of the user that we entered in, or if that person doesn't have a name on GitHub, let's just do select an option. The component we're going to use is a dashboard component which we haven't made yet, but we'll make that in a second. Then the properties we're going to pass to it, or the data we're going to pass to it is userinfo, or is the response and it's going to go in as userinfo.

After we do that, we're going to set the state. So if we go back, the state will be nice and clear for us, or nice and reset. We're going to have an error is going to be false, and the user name is going to be an empty string.

This looks great. We're doing both of these things that we want to do. The only thing we need to do now is make this dashboard component.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?