React Native Tutorial #5 - Text Inputs
Key Takeaways
Captures user input by using Text Input components in React Native
Full Transcript
our rather than gang so now we've seen how we can use states inside a component using the U state Hulk now what I'd like to do is add in a text input field so a user can type in something into that input field and then we can take that value and update our state with the value that they provide okay so first of all we need to use a component called text input and we need to import that up here at the top so I'm going to get rid of button because we don't need that anymore and instead import text import like so now you'll already see that I've created two pieces of state one of them named and set name is equal to you state and then we have a name of Sean and then the next line is age and set age and that's equal to 30 to begin with so at the bottom down here what we're doing is just outputting those two variables name and age and we can see named Sean aged 30 so what I'd like to do now is add a text input field for each one of these things right here so two input fields in total and a user can type into those input fields to update those two values so first of all I'm going to do a text component this is just going to be for a label for the first text input and it will say enter name like so and then underneath that I'm going to do a text input so we imported that is just text input like so and it's a self closing tag so let's first of all save this and take a look at what this looks like on the screen over here so we can see enter name at the top but this down here this input field we can't really see it's just a blank space however if I click into it we can see the cursor and the keyboard pop-up so it is there however we can't really see it so what I'm going to do is add a style prop to this and set it equal to styles and then we'll call this style input so dots input and let's create that down here so input and that will be an object and this is just gonna have a few simple different properties so first of all border width and that is going to be one because by default it doesn't have a border also we want a border color and that is going to be seven seven seven so kind of like a deep gray then I'm going to give this a bit of padding which is eight pixels a margin which is ten pixels to bring it away from these two text lines and then also a width of 200 pixels so if I save this now we should see something that looks a bit more like a text input over here awesome and if we click it we can see the cursor and we can type stuff in there now at the minute when we do this nothing is happening we're not taking that value and doing anything with it so I'd like to do that but first of all let me show you something else I can also add a placeholder into this text input as well and we can do that by adding a placeholder prop on the text input now what I'm going to do is format this a little bit differently I'm going to bring this down to the next line otherwise we're going to have loads of props going over and out of the screen so I'm just going to stack them instead so first of all we have the style then I'm going to do a placeholder so placeholder and I'm going to set that equal to a string and I'll just say eg John Doe okay because this is a name input so if I save that now we should see that placeholder inside the text input there it is in faint gray if I click it and start typing it deletes that and replaces it with whatever I typed awesome so now we have that placeholder we have the style what I'd like to do is detect whenever the value changes whenever a user types a new character inside this field I want to then take the current value that's inside the field and I want to update our state the name with that new value so to do this we need some kind of event listener so you know like on the bottom we have the on press prop well on an input field we can have on change text so whenever the text changes inside the import then it's going to fire some kind of function in here now we could externalize it up here like we did before but this time I'm just going to do it inline inside an anonymous function because it's just gonna be a one-line statement so all we're gonna do is call set name which is the function up here to set the state of this and we're gonna pass in a new value and that value is gonna be whatever a user types in whatever the current value inside here is now when we pass a function to on change text that anonymous function or whatever function we reference here automatically takes in the value inside the text field at that moment in time so we get access to what the user has typed in and when we set name we can pass in that current value into that function so we'll update in the state so if I save this now then hopefully it when it reloads I can type in something now watch the name change down here as I type because every time the text changes in here we run this function and we update the name uses set name and since we're out putting their name down here and it's been updated it should update in real time over here as well to reflect that change so I'm going to change this to Mario and as a type you can see it changing at the bottom as well awesome so it's updating the state with whatever I type in to the text input and this is really good because if you have a farm in the future for logging in or signing up you can gather all of the information from a user and store it in some local state and then take all of that data and make a request to whatever server you need to to sign that user up okay so it's a good way to track what a user is typing into these input fields so we have our first input field now I want to do another one for the second piece of state which is going to be the age and let me paste that down here so I just copied this and paste it down here I'm gonna say enter age instead for the placeholder I'll say eg 99 and then we want to set the age this time with the value of this input field rather than the name so let me save this and again let me enter into these two fields so first of all the name I'll type in Mario and then the age which is gonna be I don't know 25 press the tick and now we can see name is Mario and age is 25 awesome so now we've seen how to add a text import and track the information that a user types in and update the state with that data I just want to show you one or two more things I wanna see more props we can add two text inputs the first one is going to be the multi-line prop like so and this allows us to add multiple different lines inside the text input so say we've got like a blog post to write or something like that or more than just one word like a few sentences we could use the multi-line if I save this now then you're gonna notice over here it's probably not going to look any different but if I start to type and then enter it goes on to the next line and it gets bigger okay so that's one thing the next prop I want to show you is the keyboard type and we can set this equal to several different things it could be default or number pad or numeric for example if I type here numeric and in fact that should probably go to the age input not the name one because we want a number here so I could add right here the keyboard type to be numeric if I save this and then when this reloads if I go to this the keyboard that pops up is now numeric okay so there's quite a few different props we can add to text import and we are going to see more of them in the future but if you want to read more about them now I will leave a link down in the description below so you can check out all of these different props you can add to a text input field in the next video I want to move on and show you how we can work with lists of data and outputting those lists to the screen
Original Description
Hey gang, in this React Native tutorial we'll take a look at capturing user input by using Text Input components. These come baked into the React Native library.
https://facebook.github.io/react-native/docs/textinput
----------------------------------------
🐱💻 🐱💻 Course Links:
Course files - https://github.com/iamshaunjp/react-native-tutorial
🐱💻 🐱💻 Other Related Courses:
+ Complete React Tutorial - https://www.youtube.com/watch?v=OxIDLw0M-m0&list=PL4cUxeGkcC9ij8CfkAY2RAGb-tmkNwQHG
+ React Hooks & Context Tutorial - https://www.youtube.com/watch?v=6RhOzQciVwI&list=PL4cUxeGkcC9hNokByJilPg5g9m2APUePI
+ Modern JavaScript Tutorial - https://www.youtube.com/watch?v=iWOYAxlnaww&list=PL4cUxeGkcC9haFPT7J25Q9GRB_ZkFrQAc
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Net Ninja · Net Ninja · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
Related AI Lessons
⚡
⚡
⚡
⚡
Had my Frontend Developer interview with Capgemini (Application Developer) today, and I wanted to…
Medium · JavaScript
10 Frontend Developer Tools to Boost Productivity in 2026
Medium · Programming
10 Frontend Developer Tools to Boost Productivity in 2026
Medium · JavaScript
The US Frontend Engineer Market in 2026: A Data-Driven Reality Check (and the Bias That Stops Us Seeing It)
Dev.to AI
🎓
Tutor Explanation
DeepCamp AI