With React Native you use ActivityIndicatorIOS to show or hide a spinner. In this lesson we combine ActivityIndicatorIOS with our HTTP requests in order to give the user feedback about data loading.
You'll notice if I search for username, the data's loading when I click Search, but we don't get that loading in the Github bar. Even though we have that isLoading property on the state object, we're not really using it yet. Another thing, the main component is missing. If I search for a username that's not found, it doesn't give me any feedback. Let's go ahead and add those things.
First, we're going to create a variable up here called showErr. What this is going to be is it's going to be a text element which will show if there's an error and not show if there's not an error. We're going to say, this.state.error, if that's a thing, then showErr is going to be this text component with the actual name of the error there. If it's not a thing, then just render this empty view.
Down here, what we can do is we can throw showErr in there. Now, if there's an error, this will show with the error message. If there's not, then nothing will show. Also, what we want to do is, earlier we added our ActivityIndicatorIOS, we actually want to use that.
What's going to happen is you'll notice that we're keeping track of this isLoading state the whole time. We want to say if isLoading is true, go ahead and show this spinner. If it's not, then don't show it.
Down here, we're going to type ActivityIndicatorIOS. We're going to give it a few properties. This animating is a Boolean so this is going to be the true or false and this is what will tackle that if it shows. Color, let's just set it to a nice 111. Size, let's have it be large. Then, we'll end that off.
Now, what should happen is we click on this button. It runs handleSubmit. That goes and runs this function. It sets the state loading is true. Then, loading is going to be true until we get a response back from our fetch call or our fetch invocation which will then set isLoading to false, either if there's an error or if the data is correct. Let's see if this works now.
Let's go ahead and search for username. What we expect to see is when I hit Search, we should see a spinning indicator right here. There it is. Now, if I search for a username that doesn't exist, we should see "user not found." There we go.