Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,840 --> 00:00:09,360
Before we dive deeply into building our big final react project, we have to take some time to better
2
00:00:09,360 --> 00:00:11,220
understand class components.
3
00:00:12,150 --> 00:00:19,230
More specifically, we have to understand the life cycle methods that are present on them that we can
4
00:00:19,230 --> 00:00:21,600
leverage when writing our class components.
5
00:00:22,630 --> 00:00:29,890
Now, inside of our monster's Rolodex project, we only used component did mount, but there's actually
6
00:00:29,890 --> 00:00:38,020
a couple others that we need to explore because they all serve different purposes and they all trigger
7
00:00:38,020 --> 00:00:41,700
at different times in a components life cycle.
8
00:00:42,220 --> 00:00:46,210
And I will show you what that is throughout the next couple lessons.
9
00:00:47,020 --> 00:00:53,110
But before we do that, I'm just going to show you what this quick little react at project that I've
10
00:00:53,110 --> 00:00:55,300
got is about.
11
00:00:56,360 --> 00:01:02,720
Now, this is just a quick Sciarra project that I spun up the same way that we did with our monsters
12
00:01:02,720 --> 00:01:09,510
Rolodex, except I only want to use it for demonstration purposes of learning these concepts.
13
00:01:10,220 --> 00:01:14,840
Now, if you want, I have included the GitHub link to this project.
14
00:01:15,290 --> 00:01:19,100
But honestly, it's not much that you need to code along with.
15
00:01:19,370 --> 00:01:25,550
It's more for you to see and watch and listen while I explain these upcoming concepts.
16
00:01:26,600 --> 00:01:34,490
But all this project really comprises of is it's slightly modified of what the base project that ReachOut
17
00:01:34,520 --> 00:01:35,070
gives us.
18
00:01:35,600 --> 00:01:44,270
And in it, I've just modified our app component to have these two buttons that decide on what to show
19
00:01:44,270 --> 00:01:46,190
for our life cycles component.
20
00:01:46,850 --> 00:01:52,190
Now, I know this lifecycle component just looks like a bit of text right now, but I just put it here
21
00:01:52,190 --> 00:01:54,470
so that you can see whether or not it's there or not.
22
00:01:54,920 --> 00:02:01,100
And what I mean by that is whenever you click this toggle life cycles button, you'll see that that
23
00:02:01,100 --> 00:02:02,260
component disappears.
24
00:02:02,780 --> 00:02:03,080
Right?
25
00:02:03,090 --> 00:02:09,169
I'm pulling it off the DOM based on whether or not you've clicked or toggled the life cycles button.
26
00:02:10,330 --> 00:02:16,040
And then the update text button, whenever you click, we'll just put this underscore hello.
27
00:02:16,390 --> 00:02:21,790
And the more you click it, the more underscore hello's get appended to that text.
28
00:02:22,850 --> 00:02:25,400
Now, let me show you what the code looks like.
29
00:02:27,320 --> 00:02:33,930
Now, as you can see from the folder structure, it's pretty much just the base create reactor project.
30
00:02:34,850 --> 00:02:40,850
The only difference is that we have this life cycle component that will look at a little later that
31
00:02:40,850 --> 00:02:46,310
I import into our app component and then I append it right here.
32
00:02:47,090 --> 00:02:48,620
Now we'll explore this code.
33
00:02:49,250 --> 00:02:54,200
But the first thing that we need to notice is that our app, of course, is a Class-Based component
34
00:02:54,740 --> 00:03:03,410
that has a state where the initial values are this show child property that is a boolean with the default
35
00:03:03,410 --> 00:03:04,450
value being true.
36
00:03:05,270 --> 00:03:10,250
And then this text value, which is a string with the default value being an empty string.
37
00:03:13,320 --> 00:03:18,720
If you ignore the stiv in this header, this image, because these are pretty much just presentational
38
00:03:18,720 --> 00:03:25,920
HTML elements, the main thing is to look at is these two buttons and then, of course, this code right
39
00:03:25,920 --> 00:03:26,220
here.
40
00:03:27,640 --> 00:03:35,530
In this button, we have this on Click that will fire off our function that calls Set State where we
41
00:03:35,530 --> 00:03:43,060
set the show child property of our state equal to the opposite value of what it was currently.
42
00:03:44,760 --> 00:03:52,440
We do that by using this bang operator and what this bang operator does is it evaluates any bullion
43
00:03:52,470 --> 00:03:55,720
or any value to the opposite value that it is.
44
00:03:56,190 --> 00:04:04,380
So, for example, if a child is true, then I want to set the state value of show child to the opposite
45
00:04:04,380 --> 00:04:10,230
value, meaning that it will be bank true, which is equal to false.
46
00:04:10,860 --> 00:04:16,950
Bang just says give me the opposite boolean value of whatever that value is.
47
00:04:17,579 --> 00:04:22,380
So if state show child is false, then being false is equal to true.
48
00:04:23,460 --> 00:04:30,270
So that's all we do, it's a really handy way for us to toggle between a true and false state by simply
49
00:04:30,270 --> 00:04:31,870
calling this bang operator.
50
00:04:32,100 --> 00:04:39,150
So whenever you click it, it will set the value to the opposite value as the next state value.
51
00:04:40,780 --> 00:04:47,350
In this second button, we have this unclick that fires this anonymous function that will cost that
52
00:04:47,350 --> 00:04:55,480
state, that sets the state text to whatever it currently is, except adding on this extra underscore.
53
00:04:55,480 --> 00:04:55,870
Hello.
54
00:04:57,010 --> 00:05:02,140
So all we do is whenever we click it, our state text just gets more and more underscore.
55
00:05:02,140 --> 00:05:06,700
Hello, string's appended to the end of it, as we saw whenever we click the button earlier.
56
00:05:08,560 --> 00:05:16,150
Finally, we have this code here that determines whether or not to hide or show our life cycles component.
57
00:05:17,490 --> 00:05:25,050
So this, you might see, is kind of odd looking, this is actually what's called a ternary operator.
58
00:05:26,000 --> 00:05:31,970
And it sounds like a weird thing, but it's actually just in shorthand that allows us to write easier
59
00:05:31,970 --> 00:05:33,170
if else statements.
60
00:05:34,360 --> 00:05:43,570
All it is, is it takes the form of this questionmark and this Colen with different things between it,
61
00:05:43,570 --> 00:05:44,980
so you can see it as three parts.
62
00:05:45,790 --> 00:05:51,040
The first part is what we want to evaluate as true or false.
63
00:05:51,880 --> 00:05:53,170
So what do I mean by that?
64
00:05:53,500 --> 00:05:58,270
Well, if you can imagine that we had some if statement, right?
65
00:05:59,410 --> 00:06:00,070
Like this.
66
00:06:01,470 --> 00:06:07,200
This first initial if statement, right, where we evaluate something about whether or not it's true
67
00:06:07,200 --> 00:06:13,500
or false is pretty much what this is saying, and that's why we use this boolean value that we have
68
00:06:13,500 --> 00:06:15,990
in our state as true or false.
69
00:06:17,480 --> 00:06:26,240
So I'm saying if this state does show it is true, then I want to either render this or this.
70
00:06:27,530 --> 00:06:35,390
So this first thing that we see after the questionmark is what gets evaluated if show child right.
71
00:06:35,430 --> 00:06:37,620
So whatever is on this side is true.
72
00:06:38,270 --> 00:06:42,890
So if your child is true, render the life cycles component.
73
00:06:44,300 --> 00:06:49,100
If this starts to show child is false, then render not.
74
00:06:49,730 --> 00:06:53,690
So you can see this last section here pretty much being what goes into the full statement.
75
00:06:54,560 --> 00:07:03,470
So you can see this as what goes here, this as what goes here and this as what goes here.
76
00:07:05,270 --> 00:07:12,410
So it's pretty much just a really handy and quality of life coding syntax that allows us to write if
77
00:07:12,410 --> 00:07:15,520
all statements in a much easier and readable way.
8196
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.