Build React Apps | App State Management | Practical Examples #03
Skills:
React85%
Key Takeaways
The video demonstrates how to build React apps with a focus on app state management using practical examples, covering topics such as React state management, props, and context API.
Full Transcript
Hezbollah guys are all about doing welcome to the third episode on learning react course series so as you watch the lottery tutorial organic covered is the components and what are actually we have components how we can use them efficiently and why they are very important for like managing the application stay tuned for making like briefly or flexible reacts applications and take full control over our user interface so yeah I'm gonna after explaining what is a component that here can manage it and what is the rendering method in case structure and like this component mount method events but when its action will start today we're gonna talk about more briefly about the stage so what's actually the state does how we can manage our state or our like UI state which is the react component stage like we share wait we deserved is a voice or hide it or change some kind of a DMM archer things on like specific type of events so what we're going to do is just create like a couple of like I don't know an input then when we try to enter in the text without input we change the our our text so we display it right out of the box so as you can see we have a component in here I have I'm calling just here oh you can call whatever you want in here whatever swibble for you but I'm just using a head or component over here so I'm going to do is just go ahead let me just call this like an H and H to learning for example learning stage managing so manage minutes okay okay state management over here and we're going to do is just create another gap so I'm gonna go through and creating you another Devon create an h3 and that H we I'm going to say my hobby is my hope it as and we re Ariana and what we're going to do is create an input and that input what we can do end it on that particular input is into our orbit in this particular text over here is going to change immediately so whenever we change type in or as soon as we type in this text is going to be updated so we're gonna we can actually implement that and see how this pretty much works so I'm gonna have like an input and with a place holder of I don't know your your hope so your pulpit okay and I'm gonna have I'm gonna give it a class name I'm using by the way push drop over here just for making this sound looks a little bit preachy so I've gone into the indigo HTML and other the link to a bootstrap CDN so just include the fourth version of bootstrap over as you can see you can you can use the okay like search for bootstrap CDN graphic over here so after including that I'm gonna just a form control which is like an input class name for the from from bootstrap so you can just tell this a bit and what I'm going to do over here is we actually going to do is add a new event listener so whenever this change so which means whenever we type and do this input we need to change this particular thing so for now if we control SAS and go and open up this indoor the browser so open opening default browser so we need here to open up and give all browsers can see we have learner state management to my house and my husband so for just assigning management's one I'm going to pretty much they'll go ahead using and one thing I haven't no I haven't told you about is like if you have notice I'm using class name in like instead of class so one way he usually declare and this human element we use the class attribute in order to give it whatever CSS classes or predefined CSS classes but for react you tell it is the class name so he changed from class to class name why that because react doesn't know about the class it tried to alter links and make it class name and all that you like recognizes when parsing this thing so don't make sure to put in like the class under the class possibly put ever whatever process between here like normally do so you change it from class to class name and you're good to go now I'm just gonna say this is like you're going to be a container and pretty much yeah this is it and under here is just gonna give it also class need and mix it text Center just to Center this text so yeah so go back over here as you can see we've got a little bit more better style over here so what I want is when I type something over here I need to change this text so I make it automatically get change and prints whatever we have a hobbit over here printers and do this h3 element or and like this particular text or this particular continue so how we do that using the set state and react state so under view further for each react component it has a particular state object so this state object holds all every single like variable but we need indoor our element or our under our components so this light could be any like user names like I don't know a message is some some kind of a counter that accounts if you have like an components when you press the button it adds one or count how many process you you have done into this button and how many process you've pressed to the bottom actually and you can everything you need to keep updating or everything is going to be updated it needs to happen or you need to put it into this state so the states objects for this particular case we're gonna put a text I'm just gonna be empty so this text is gonna hold our Hall beds as soon as we enter it or we entered under the actually the the input over here so what we're going to do is we're just you can ensure that you can declare it or no it's this like very optional for you since we can use it both ways on JavaScript there's no no issue for us but make sure to enter it in here or make it just an empty in order to for you to make it easier to understand or for any other developers make it for them to like easier understand what actually you are doing away now how we check whenever we type and a text here using the on change method so as I've told you to use math or to use events we use them as inline event so let's say when you have a button and you want to collect the button we use the onclick event it's an inline event so you can't tell that like he using javascript to reference that elements and on using jQuery or something on click on stuff know reacts just uses laws like can inline arguments and here you're passing in the the callback you want up a sign or you want to like run whenever this event has being triggered so yeah on the on change what we want is to change that so let me just go back or go into our components class and add a new method and this method is going to handle input change for example and yeah it's going to be a pretty simple method and endure each component or each like elements on each event what is going to one when it's runs this one it gonna pass in automatically by JavaScript you're gonna passed in an event handler so it's gonna pass in an object in here gonna hold a bunch of informations about our elements the press elements or the occurrence of the target element that we are actually working on on the particular event or when the particular event has happened or has triggered now on change how we tell us he was in this not handle event input that change any need to bind this so might some of you asked why we bind this and that particularly what because this events is gonna call this callback over here so let me just yeah this callback over here the handle change is not going to be call it and do this context so the context of our class so what it does when this event has been triggered it just gonna kill ahead and register of course just of the handle import and whenever the events happens it's just gonna call the hinder input change but it doesn't know about the existence of this class so what we need is to bind this context which means to find the the header class contacts into it so whenever we use this enter this method over you it does know this it refers to whom which class it rivers to unless if you don't do that it's just gonna give you a really really weird error so make sure just to do it over here or just to bind this in order to make everything works for you know what we actually need to do is to reference that so it just go ahead and cross on the blog and to see actually what we have entered so dots even thoughts target and the target represent the target element stop this event is running off in this case is the input so if you are putting this event into an h3 is going to be the HV 83 itself and so on and so forth you can learn more about that into the native JavaScript API and the targets and we need to grab a in the bomb now just close this control s for saving we go back into our let me just control s or refresh the page and open up the developer tools for seed well actually going to be printed out into the console so here we point something I just can see each time I'm friends for example my it changes immediately so whenever we type a character it changes that and add this character so it happens whenever a change is like happened into this Caray or a new character has been assigned into the input I mean not array so yeah this is how we work start what we need is just keep track of these tags then updates this particular one so my some of you might think we can just access this elements and change it but it doesn't it won't happen that way what we need is to tell react to change this one the this variable over here and when it changes this this variable over here it's need to we render everything so we need to tell two things to change this particular text over the h3 element and re-render the component why we're rendering the component to tell it to take or to make the changes apply so you apply the changes into the actual Dom so reacts makes this super super simple for us by adding a new method into each component so since we are extending from the react components the base component class what we can use is the set class or set state method what it does this one it is going to change our state so what we're going to tell it is the just passing an object it does it's actually gonna know which or the state object autumn please sir we must use this top stage or he must call your stage variable state because it just kind of automatically looks for the state naming convention on the name of the state and here you pass me the new object you wanna run so in this particular case we're gonna just put text and you put in the new text so you access target the value and you are good to go so maybe have like another video going here for examples called counter and it's just like zero so like some of you might ask do we really need to call the other variable and safe to zero again not really so it's just gonna take thee or you just need to pass in the variable you enter the objects in here the attributes that you want to change only so let's say you have a hundred of attributes into this slave state and you only need to change the text so you only need to pass in the new text that has been or you need to alter you need to change over here do not care about the other things that you don't need to change and all react is gonna do the rest of the job for you now set the state this is gonna set the text into the a target of value and we argue together so here let me just get rid of this counter over here and just kind of set the stage and in order to let this happen what we can do is need to reference dance so we need to call this don't state so this spot stage footsteps so in order to tell it to render something into things you need to use curly braces and put whatever variable in here so this how GS acts works in the react so for this for save objects and we need when actually whenever we alter or change the text when setting the stage this is going to be changed automatically by react and we're gonna just update everything and one Mon or one another notice I need to make sure before going in seeing what's actually going to change is this the set stage or this talk says states method is going to automatically tell react to re-render the whole component so we're gonna just apply the changes that we have over here so they're gonna apply the changes to the doorman actually so control s for saving and go back over here refresh everything and here the try as you can see we've got immediately nothing because we are just passing in and I know I know unlike attributes what we can do is so ensure your puppet for example so control us again in order to show that on the beginning so now let's try my Hobbit as you can see it changes immediately whenever we type in a new character and someone and so forth as our programming or something like this any of us can see the text changes immediately and gets updated automatically by itself and that using only the same stage method it cannot do the job for us or perfectly know what's good see will like just about like I don't know empty things just gonna go what we can do in here is check if an empty or now so it's a very quick hack around that so what we can actually do is go ahead and add in an if statement and say if this pop state dot text equals an empty text then what you can do as this boxset state of text and you put in an empty or you put in injure your hobbit type okay and else you can enter the newly inserted text so in this particular case is going to be e to target or fall you and this pretty much is going to work pretty much fine so I don't mean this box state of text what I mean is I'm a little bit confused over you so let me just change that so I need to check into this target or value if it's equals to know which means if it sees an empty thing so we just enter your hobbits and stuff like this so control us again go back refresh and enter your hobbits so your hobbit it changes so whenever now we delete ever then as you can see we get entry hall roberta back to us so automatically and it's like pretty much how stage management works it's very very simple and very very basic now another thing i want to mention before the end of this week is toriel is that you can probably let me just add another DEATH and under that Devin goes I'm gonna I don't know like about an h2 of a counter so how we can add the counter or something you're gonna be pretty much the same thing but I just want to add it for more demonstrative purposes so I will add a counter variable into this estate and just gonna sell it to zero and each time we press the button that I'm just going to like create right now so whenever we type this or press this button we're just gonna increment this counter in here so we press the button we plus one into the counter so which means counter plus plus now let me just go and class name equals I'm just gonna use BTN so I'm using bootstrap by the ring here so we can success ambi TM - so just busy and success and you're good to go so I'm looking increments you can call increment plus plus and here we can use this top state counter and make sure to whenever you need to print in any variable any variable exist in your class class contacts you can put it between curly braces as you can see in here and then you prints whatever or you put whatever variable name and not to get printed between the HTML elements you want to get printed on so yeah it's like a basic how it works now we need to add in an event or we need to add in the on click event handler so just type in the on click the event name make sure to use the onclick on Sycamore case so we start with the a lowercase and then an uppercase for the next sentence over the next word which is the click now here you can use us can see you we have used like a predefined handlers under our class like the class context that we have just predefined then just give it the this handle change it's just gonna work here what we can do is declare an inline callback so it gonna just run whenever this on my own click happens or the on the actually on click event so what we can call this is just very basic using the ESX syntax or you can use a function in here pretty much the same function it just gonna work fine but I'm gonna use ESX and sucks so you're just gonna run immediately with no problems so the on the click what we're gonna do is just increments the counter so for that we're gonna also have an event over here so you can pass in any events for now but for us we don't need it anyway and what we need is to set the stage so we need to access the counter then plus one to the counter so we can do is this top set stage and under this stage as you can remember we need to pass in the new stage objects that we need to change but in this particular case what we need to do is counter plus one which means that we need to know about all the previous stage of the counter so let's say that we have counter is equal to zero so we need to get the previous counter plus one in order to get the counter equals one and so on and so forth so what we can do that react also provides us with a really like with another with another expression or we know with another definition of this this date that you can pass in on a callback rather than rather than ANOVA just over them so they scroll back what is hold is the previous tease so the previous stage asked the arguments of the call back then you passing in the callback so under this callback you put in you kidding apprentice season into the parentheses you put in the objects so the new objects or the new state objects that you want to undo so make sure this is like the previous state under the set state callback so we're just gonna kid takes or callback as you can seen here I'm putting into callback and enter this callback I'm hearing a previous states and other arguments so react is going to automatically pass us the previous state so we can use it to increment or to do whatever things that we need to and here the new object so we can say encounter equals previous state doc counter so make sure or like always remember that here it is pretty much the same state the same state object in here so it does have a counter attribute or a counter variable that we can access so we say counter and what we can say is +1 so we each sign we set the counter into the previous state culture plus 1 and this is how we tell it to work so you can use the previous stage or you can use the same procedure that we have to use a mock up here we will now go back or something Allah or you can use the previous States or previous definition with no no callbacks only local objects that you can pass in into the set state method so you can see this how it actually works now control s for saving we go back and we refresh our things refresh the pages can see we get 0 now increments 1 2 3 4 and so on and so forth can see it worse each time we get a previous state counter then plus 1 and so on and so forth each time so yeah this how pretty much it works this the like the two definitions of the set state with a callback or with an object's so both of them has the one of the same or actually I don't know the same object that you need to pass same but what the difference one has the previous States is a callback and the other one only the objects and stuff like this so you can use each one in a different situations like dependently with with no problems so in whatever situations you need you can you can use whatever set state definition because watching that was it for this s status you can see that how we can manage states and how we alter between things and variables and all those kind of things using the set stage method for me apps and that has been extended from the react based components I think is watching again I really do hope you enjoy if you have problems issues if things you wanna like ask me about the comes below I would be very happy to answer your questions yeah as I've said or I will say said I would catch you in the next video tutorial
Original Description
Written Tutorial:
http://go.ipenywis.com/ipenybe991
--- Official Links----
Official Website: http://ipenywis.com
Official Facebook: http://go.ipenywis.com/ipeny4907
Official Twitter: http://go.ipenywis.com/islem9a03
---Support US---
By Becoming a Patreon: http://go.ipenywis.com/ipeny5024
http://go.ipenywis.com/ipeny5c0e
Playlist
Uploads from CoderOne · CoderOne · 35 of 60
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
▶
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
Laravel CMS | N-01 | Getting Started and Environment Set Up
CoderOne
Laravel CMS | N-02 | Routes, Controllers and Views
CoderOne
Laravel CMS | N-03 | Dealing With Assets and Page Layouts
CoderOne
Laravel CMS | N-04 | Migration, Models and Adding Categories
CoderOne
Laravel CMS | N-05 | Showing Added Categories, and Session Messages
CoderOne
Laravel CMS | N-06 | Edit\Remove Categories using AJAX and Popups
CoderOne
Laravel CMS | N-07 | Add New Post
CoderOne
Laravel CMS | N-08 | Creating a Basic Laravel Blog
CoderOne
HTML5 Canvas For Beginners | Getting Started | #01 with Webpack
CoderOne
HTML5 Canvas For Beginners | Drawing Basic Shapes | 02
CoderOne
HTML5 Canvas | Drawing Text With Circles | Part 01
CoderOne
HTML5 Canvas | Text Animation and Circle Collision | Part 02
CoderOne
Complete Guide To Web Development In 2018
CoderOne
AJAX & JSON Explained | Examples
CoderOne
Visual Studio Code Web Development Setup and Extensions
CoderOne
Electron Build Desktop Apps HTML/CSS/JS - Getting Started 01
CoderOne
Electron Build Desktop Apps HTML/CSS/JS | Creating Windows and Processes 02
CoderOne
Electron Build Desktop Apps HTML\CSS\JS | Dialog, Sessions and Cookies 03
CoderOne
Electron Build Desktop Apps HTML\CSS\JS | Custom App Menu & Context Menu 04
CoderOne
Electron Build Desktop Apps HTML\CSS\JS | Package, Build and Deploy your App 05
CoderOne
Node.js Brief Beginners Introduction #01
CoderOne
Node.js How Modules Works & NPM #02
CoderOne
Node.js Working With Events and Event Emitter #03
CoderOne
Node.js File System (Read and Write) FS 04
CoderOne
Node.js Create a Basic Server Using Express 05
CoderOne
Json Web Token Authentication JWT Explained Securing API
CoderOne
CSS Flex Box Design | Practical Examples |
CoderOne
API How It Works With Practical Examples
CoderOne
MongoDB Getting Started 01
CoderOne
MongoDB Working with APIs and Practical Examples
CoderOne
Create a Command Line Interface (CLI) Using Node JS #01
CoderOne
Create a Command Line Interface (CLI) Using Node JS 02
CoderOne
Build React Apps | Introduction And Getting Started #01
CoderOne
Build React Apps | Working With Components And Multiple Rendering #02
CoderOne
Build React Apps | App State Management | Practical Examples #03
CoderOne
Build React Apps | Getting Started With Flux (Intro) #04
CoderOne
Build React Apps | Create a Clock Timer Using React 05
CoderOne
Build React Apps | Create a Clock Timer Using React 05 | PART2|
CoderOne
CSS Preprocessors Getting Started | SASS and LESS | Practical Examples
CoderOne
Regex Introduction | Learn Regular Expressions 01
CoderOne
PHP VS Node.js Which is Best For Web Development
CoderOne
Drag and Drop Using Native Javascript 01
CoderOne
Drag And Drop Using Native Javascript 02
CoderOne
Master Git (Version Control) in One Video From Scratch
CoderOne
Let's Learn The New Javascript ES6 Class Syntax
CoderOne
Let's Create A BlockChain On Node.js
CoderOne
Best Online Code Editors For Web Developers
CoderOne
Let's Create a Modern Login Form on React #01
CoderOne
Let's Create a Modern Login Form on React #02
CoderOne
Laravel CMS | N-09 | Admin Registration
CoderOne
Laravel CMS | N-10 | Login and Authentication
CoderOne
Let's Create a Twitter Bot (Listen and Retweet)
CoderOne
Rapid Webpage Creation With Emmet (HTML & CSS)
CoderOne
Create Popups and Modals Using Native Javascript, HTML and CSS
CoderOne
Promises and Callbacks on Javascript With Practical Examples
CoderOne
Create Collapses using Native Javascript, HTML and CSS
CoderOne
Let's Learn Typescript (Javascript Devs) | Getting Started 01
CoderOne
Let's Learn Typescript | Interfaces and Generic Types (The Typing System) 02
CoderOne
Let's Learn Typescript | Work With Classes, Modules and Enums 03
CoderOne
Let's Learn Typescript | React and Webpack With TS 04
CoderOne
More on: React
View skill →Related Reads
📰
📰
📰
📰
Stop installing giant icon packages — write SVGs as source with a CLI + MCP
Dev.to · arialus
Frontend Engineers Are Quietly Becoming the New Overhead.
Medium · AI
Building an Interactive Fashion Hero with HTML, CSS & GSAP
Dev.to AI
Out-of-Order HTML Streaming Moves from JS Frameworks into the Browser
InfoQ AI/ML
🎓
Tutor Explanation
DeepCamp AI