React Native Tutorial #5 - Text Inputs

Net Ninja · Beginner ·🌐 Frontend Engineering ·6y ago

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 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
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…
Prepare for a frontend developer interview with Capgemini by reviewing JavaScript fundamentals and practicing common interview questions
Medium · JavaScript
10 Frontend Developer Tools to Boost Productivity in 2026
Boost frontend productivity with 10 essential tools for modern web app development
Medium · Programming
10 Frontend Developer Tools to Boost Productivity in 2026
Boost frontend productivity with top 10 developer tools in 2026
Medium · JavaScript
The US Frontend Engineer Market in 2026: A Data-Driven Reality Check (and the Bias That Stops Us Seeing It)
US frontend engineer hiring demand peaked in 2022 and remains flat-depressed in 2026, contrary to common assumptions
Dev.to AI
Up next
The masks we wear | Zora Krstić | TEDxLuxembourgCity
TEDx Talks
Watch →