React Native Tutorial #4 - Using State

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

Key Takeaways

Demonstrates how to use state in React Native components

Full Transcript

okay then gang so hopefully now you're a bit more comfortable with this style of syntax using view components and text components its output stuff to the screen as well as styling those using a stylesheet in react native as well now I'd like to take this one step further by adding state into the mix so that we can output some dynamic content to the screen as well inside these components so to do this we'll be using a react hook now if you don't know what react hooks are at all I would suggest watching my playlist all about react hooks in context first of all because I'm going to be using them quite frequently in this series but basically Hawk's give us a way to use special functions to tap into certain features in the react library now in our case we're going to be using the use state hook which means we can use states inside a functional component which is what this is so first of all to use that hot we have to import it up here so inside curly braces let's say use States so we can import that from the react library and then let's create a bit of state using that hook so we do this by saying Const and then in brackets or square brackets so an array we have the actual piece of state which I'm going to call name and then the second item is going to be a function we can use to update that bit of state in the future called set name so that is going to be equal to the use state hook and inside here we pass int the initial value of this bit of state so I'm just going to pass in Sean so to begin with this name variable right here will be giving the value of Sean that is the bit of states we're creating and in the future if we want to update that value then we can use this function to do that okay so what I'm going to do is inside this first text component is just say my name is and then I want to output some dynamic data and to do that we use curly braces and then whatever variable we want to output now the variable I wanna output is this thing here named so let's paste it in and save that and then hopefully we should see that over here on the screen my name is Sean awesome so we're taking that bit of state now and we're at point it in the text now what if I want to change this in the future well I can do that using this function so what I'll do is create a button over here we've not seen buttons yet and when we click on that button I want to update this to a different name so down below this second text widget right here I'm going to create another view and this view is going to be a container for the button just so we can style the button a little bit so I'm gonna say style is equal to Styles dot button container and we'll create that style in a second but inside this view we now want to create a button now react native comes with a button component built in to the library so let's import that up here button and we can add it down here inside the view like so and we don't have a button with an opening and a closing tag much like you might think and then place the text inside here like click me that would be the logical thing you'd think would happen right but no in react native button is a self closing tag or a self closing component and what we do is pass props to specify what text is going to be on the button itself and that prop is a title prop this will be a string and it's just going to say update state or update name or something so if I save this then preview over here we should be able to see this button down here and this is the default style of a button now the reason I place this inside a view was so we could add styles to the view itself now we can't add a style property to the button itself I couldn't do that react native doesn't allow us to do that and we can get around that in the future by creating our own custom buttons the react native library comes with the bottom component and we can change it a little bit by adding some different props here change the color and we'll see that later on but we can't actually add a style property to it so I've surrounded it with a view so I can add the style property to the view and then we can style that so if I come down here I could say button container now that would be an object and all I want to do is give this a margin top of 20 pixels just to bring it down a little bit so I'm gonna save that and we should see it brought down a little bit down here there we go okay so now we have this button if I click on it at the minute it doesn't actually do anything and the way we can hook up some functionality to a button is by adding an on press prop so unpress is going to be equal to some kind of function now we could add the function here directly so I could do an anonymous function and arrow function and then you know do something here but I'm not going to do that instead what I'm going to do is kind of externalize this function I'm going to create this function up here because when you add you know quite a bit of a logic to a function it's not always the best idea to do it in line because it becomes messy instead sometimes I like to separate the functions out unless it's just a quick one line one so what I'm going to do is create a new constant and call this click Handler and this is just kind of like a convention or we could call this press handler it's up to you you know we do the function and then handle after it this is a convention react uses you don't have to call it this that is going to be equal to a function and it will be an arrow function in our case and inside here we're just going to use the set name function up here and then pass in the new value so I could change this to chun-li for example and what this will do when this function is called is use this function to update this bit of state right here and the new value of that bit of state is whatever we pass in to the function now again we can't call this function whatever you want you could call it boot if you want and then you'd invoke boo but that obviously makes no sense whatsoever again this is just a naming convention whatever the value is whatever we've called it name and then put set before it to set the name so now we need to grab this function and put it in here so that when we press a button it looks on this unpress prop and it sees that we're referencing this function and it goes ahead and fires that function for us and updates the name when the name updates it's going to re-render wherever the name is output so we should see update in real time over here so let me save this now so it updates we should see shown to begin with because that's the initial value if I click on the button now then we can see it changes it to chun-li awesome now we can use as much states in a component as we want we don't have to just use this once this hook if I wanted to create another bit of state I could do so I could say for example Const and then this time we'll call it person and sets person is going to be the function to change that set it equal to use state to create another piece of state and this time instead of just being a string I'm going to make this an object and we can do that we can pass any kind of variable type into the state a boolean an object a string and array it's entirely up to you the first property is going to be name and that will be Mario and the second property will be age and that will be 40 so now we have this second piece of state person I want to output that down here inside this second text component so I'll say his name is and we want to output a variable so person and then we want the name property on that person dot name so we're grabbing this thing right here so after that we'll say and his age is person dots age so all we're doing is grabbing this person bit of state and then the name and the age and if I save that now then we should see over here that that is going to show hopefully his name is Mario and his age is 40 again we could change this if we wanted to by adding in the set person function in the click handler so let's do this set person and then inside a new value which is going to be an object the name will be Weegee and the age is gonna be 45 really don't know who's older of Mario or Luigi just a pas save this and then if I click the button then it's gonna update the name first of all to chun-li and then the second piece of state this object to Luigi and 45 awesome so that's how easy is to use a bit of stay or several bits of state inside a react native component very similar to react from web so now we know how to do that in the next video we'll take this one step further and look at text inputs to get information from a user

Original Description

Hey gang, in this React Native tutorial I'll show you how we can use tate in our components. This is very similar to how we would use state in a standard React web app. ---------------------------------------- 🐱‍💻 🐱‍💻 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 →