This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

React Native: State and Touch Events

7:07 React lesson by

In React, components manage their own state. In this lesson, we'll walk through building a component which manages it's own state as well as using TextInput and TouchableHighlight to handle touch events.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

In React, components manage their own state. In this lesson, we'll walk through building a component which manages it's own state as well as using TextInput and TouchableHighlight to handle touch events.

Avatar
MyGov

Hint: It would be easier if in the constructor you would initialize the "username" param with your github username instead of blank. Otherwise, throughout this lesson, you will have to retype it over and over again.

Avatar
Tyler

Though that would have saved some typing, it might have confused some people starting out with that initial state. A better solution I should have done in the video is to change the user I used rather than using myself every time.

In reply to MyGov
Avatar
Tiago

Isn't is possible to use => to replace the bind function in ES6 ?

Avatar
Tyler

Yup. I always use =>, not sure why I didn't in the video.

In reply to Tiago
Avatar
srhise

What syntax highlighting plugin are you using?? I love that red.

Avatar
Michael Lumbroso

Quick question : I thought React provided autobinding => why not {this.handleChange} instead of {this.handleChange.bind(this)} ?

Thanks for the answer

Avatar
jm90m

With the current version of React Native, I was not able to get my app to display. I had to remove "justifyContent: 'center', alignItems: 'center'," from container styles in 'index.ios.js'

Avatar
Raunaq Singh

That wasn't React providing autobinding—it's because arrow functions are auto-bound to the lexical scope. Whereas is normal functions that isn't the case.

In reply to Michael Lumbroso

In this review we'll walk through finishing our main.js component which is this component right here. Notice it's going to have some text, it's going to be able to have an input box which takes in a user name, and then what we need to do is we need some way to capture this touch event, so that whenever someone clicks on search it will then take us to the next component, or the next view.

What we're going to need to do is we're going to need to use three more React native components, textinput which allows us to capture input from the keyboard, touchable highlight which is going to allow us to capture a touch of it and do something with that, and then activity indicator ios which will allow us to show or hide a spinner based on some Boolean that we set. The difficult part about building UIs is managing state.

That's something React does a really good job with. The principle that react takes is that every component will manage its own state, and it can pass that state and any piece of data down to its children components, which can then use that.

What we'll do inside of our React component class is we're going to create a constructor. All this is, is an E6 thing. It will get passed some properties, then what we'll do is we're going to call our react component, or the thing we're extending, which is our react component constructor and we're going to pass it props. Then all we'll need to do is set the initial state of this component.

You may have noticed as we're doing the example, is this component is going to manage a user name which is the thing that we'll type into the input box. It's going to manage an isloading Boolean which will allow us to toggle that spinner, and will also manage if false Boolean, which will allow us to show an error message if something bad happens.

Now I'll modify our UI a little bit. Instead of this being testing the router, let's change this to the title which is "Search for a Github User." The style it's going to take is style.title which we made up here. Then what we'll need to do is we need to create a new input field, but we need to tie this input field to our state.

So the style it's going to take is styles.searchinput which is made above, and then what we'll need to do is the value of this input field is going to be bound to the user name in our state, and then whenever this input field changes, or whenever someone types a new letter, what we'll need to do is we'll invoke a function that we haven't made yet called handlechange which will be able to take in, or which will be able to capture the value that's in the input field, and then update our state with whatever that value is, or whatever that text is.

Let's go up here now and in our class let's create a new function called handlechange. It's going to receive an event, and then we'll use this.setstate, the state of this component, or the user name state of this component. Our event.nativeevent.text. Now whenever someone types something into the input field, or they change something with the keyboard, our state will be aware of that and it will update itself.

You're asking yourself why we're doing this bind thing here. All bind does is it returns us a new function with the this keyword bound to whatever you pass in. If you're not familiar with the this keyword, or binding, or call, or play, or anything of that stuff, just know that when we do this, it makes the this keyword in handlechange be appropriately context to the this keyword that we want.

Regular React with ES6, React will go ahead and autobind all of the this keywords for you, so you don't really have to worry about this. But because we're using ES6 React doesn't do that, so we need to say, "Hey this keyword here, we want it to reference the same thing as this keyword out here."

Now let's go ahead and make our search button. What we'll use is touchable highlight component, and we'll have a style of style.button which we made above. We'll have an onPress attribute, what this will do is whenever someone presses on this button, it will go ahead and run this handlesubmit function that we'll create later, and then we'll have an underlay color so that whenever someone is pressing on the button the background will go white.

Let's go ahead and nest a text value under here of search, and add a style property on here as well of styles.buttontext. We'll close our touchable highlight component.

Now let's go and let's create our handlesubmit function. We're going to add handlesubmit right here. If you think about the purpose of this handlesubmit function, it's going to do a few things. It's going to update our indicator ios spinner, it's then going to go and fetch data from Github, get the user's Github information, and then it's going to reroute us to the next route passing in that Github information.

We'll talk all about fetching data using React natives, in this video is what we'll do is we'll go ahead and update the state of our loading, our isloading property on the state to true. Then what we'll do is let's just console.log submit, and then we'll pass along this.state.username. We can see if this is really working, so let's verify if this is working. We have an error main.js line 73, that should not be there. All right.

What I'm going to do now, we're comfortable logging something, but we obviously don't have a console in the iPhone simulator, so I'm going to do command-D to open up this debugger window, it opens up the console here. What we expect to happen is when I hit search, there you go. We submit...

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