Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,070 --> 00:00:04,200
Reacting to events is an important
2
00:00:04,200 --> 00:00:05,280
first step,
3
00:00:05,280 --> 00:00:08,300
how can we now change what shows up on the screen?
4
00:00:08,300 --> 00:00:10,260
Well, we want to change the title
5
00:00:10,260 --> 00:00:11,820
when the "button is clicked,
6
00:00:11,820 --> 00:00:14,400
so this is one thing we could do.
7
00:00:14,400 --> 00:00:15,990
We could create a variable
8
00:00:15,990 --> 00:00:17,280
with let,
9
00:00:17,280 --> 00:00:19,870
which we may rename title,
10
00:00:19,870 --> 00:00:24,870
and initially that holds the value we find in props title .
11
00:00:25,410 --> 00:00:28,240
And then we use this title variable down there
12
00:00:28,240 --> 00:00:29,870
in our JSX code
13
00:00:29,870 --> 00:00:31,263
to output the title.
14
00:00:32,340 --> 00:00:35,650
Now if I do that we see the same results as before,
15
00:00:35,650 --> 00:00:38,220
so why am I doing that.
16
00:00:38,220 --> 00:00:40,470
Well since it's a variable,
17
00:00:40,470 --> 00:00:44,270
we could change it whenever the click handler executes.
18
00:00:44,270 --> 00:00:46,120
So whenever this button has clicked,
19
00:00:46,120 --> 00:00:48,690
we execute this click handler function
20
00:00:48,690 --> 00:00:53,410
and in there if we now change title to updated,
21
00:00:53,410 --> 00:00:56,580
then we should see that change title
22
00:00:56,580 --> 00:00:58,160
on our screen, right?
23
00:00:58,160 --> 00:01:00,200
Because we are outputting title down there,
24
00:01:00,200 --> 00:01:01,800
we're changing it up on a click
25
00:01:01,800 --> 00:01:04,970
we should see that value here in this place
26
00:01:04,970 --> 00:01:06,543
after such a click happened.
27
00:01:07,720 --> 00:01:11,140
Well, let's save everything and give a try.
28
00:01:11,140 --> 00:01:14,500
If we go back, you'll notice that if I click change title ,
29
00:01:14,500 --> 00:01:16,010
nothing happens.
30
00:01:16,010 --> 00:01:19,440
I can hammer this button and nothing happens.
31
00:01:19,440 --> 00:01:23,030
This title here never changes,
32
00:01:23,030 --> 00:01:25,270
so why is this happening?
33
00:01:25,270 --> 00:01:28,103
Is something wrong with our click handler?
34
00:01:28,980 --> 00:01:31,360
Well this function is getting triggered,
35
00:01:31,360 --> 00:01:34,050
I can prove this, if I console lock
36
00:01:34,050 --> 00:01:35,600
the title here
37
00:01:35,600 --> 00:01:37,090
after we changed it,
38
00:01:37,090 --> 00:01:40,330
you will notice that if I now go back and reload,
39
00:01:40,330 --> 00:01:41,940
if I click change title,
40
00:01:41,940 --> 00:01:45,600
we see that updated title text here.
41
00:01:45,600 --> 00:01:47,560
So it looks like it worked,
42
00:01:47,560 --> 00:01:50,070
the click handler clearly executed
43
00:01:50,070 --> 00:01:52,770
and title clearly was changed.
44
00:01:52,770 --> 00:01:55,500
Otherwise we wouldn't see that new value
45
00:01:55,500 --> 00:01:58,050
which we assign here in line 13
46
00:01:58,050 --> 00:02:00,790
once we log it in line 14.
47
00:02:00,790 --> 00:02:02,950
So if the function executes
48
00:02:02,950 --> 00:02:04,660
and the title is changed,
49
00:02:04,660 --> 00:02:08,160
why don't we see that reflected in our DOM,
50
00:02:08,160 --> 00:02:11,009
after all we are outputting the title down there
51
00:02:11,009 --> 00:02:12,373
in line 21.
52
00:02:13,950 --> 00:02:14,783
Well,
53
00:02:14,783 --> 00:02:17,430
simply because react doesn't work like this.
54
00:02:17,430 --> 00:02:20,010
And that's where now I have to dive into
55
00:02:20,010 --> 00:02:23,620
how react actually passes the JSS code
56
00:02:23,620 --> 00:02:25,390
and how it considers that
57
00:02:25,390 --> 00:02:27,500
and how it brings it on to the screen.
58
00:02:27,500 --> 00:02:31,240
Though I will have an even deeper dive later in the course,
59
00:02:31,240 --> 00:02:34,930
but there's one key thing which you have to know right now,
60
00:02:34,930 --> 00:02:39,180
keep in mind that your component is a function.
61
00:02:39,180 --> 00:02:40,610
This is a function right?
62
00:02:40,610 --> 00:02:43,260
Your component is just a regular function,
63
00:02:43,260 --> 00:02:45,880
the only special thing about that function,
64
00:02:45,880 --> 00:02:48,580
is that it returns JSX.
65
00:02:48,580 --> 00:02:50,520
Now since it's a function
66
00:02:50,520 --> 00:02:52,360
someone has to call it,
67
00:02:52,360 --> 00:02:55,130
and you might notice that we never called
68
00:02:55,130 --> 00:02:56,860
our component functions,
69
00:02:56,860 --> 00:02:59,780
instead we just used these functions
70
00:02:59,780 --> 00:03:03,453
like HTML elements in this JSX code.
71
00:03:04,810 --> 00:03:06,790
Well, the thing is,
72
00:03:06,790 --> 00:03:11,410
under the hood this is almost like a function call.
73
00:03:11,410 --> 00:03:14,630
By using our components in JSX code,
74
00:03:14,630 --> 00:03:18,770
we make React aware of our component functions.
75
00:03:18,770 --> 00:03:19,920
For example here,
76
00:03:19,920 --> 00:03:24,070
we make react aware of the expense item function.
77
00:03:24,070 --> 00:03:27,930
And whenever react evaluates this JSX code,
78
00:03:27,930 --> 00:03:31,330
it will call these component functions.
79
00:03:31,330 --> 00:03:34,610
And these component functions stand to return JSX code,
80
00:03:34,610 --> 00:03:36,830
which is all the evaluated,
81
00:03:36,830 --> 00:03:40,600
up until there's no more JSX code to be evaluated.
82
00:03:40,600 --> 00:03:43,620
So react keeps on calling any component functions
83
00:03:43,620 --> 00:03:45,640
it encounters in JSX,
84
00:03:45,640 --> 00:03:48,250
then calls any functions that those
85
00:03:48,250 --> 00:03:49,790
functions might have returned
86
00:03:49,790 --> 00:03:52,730
so any elements those components might have used
87
00:03:52,730 --> 00:03:57,330
in their JSX code until there are no more functions left.
88
00:03:57,330 --> 00:03:59,630
So in the case of expenses.js,
89
00:03:59,630 --> 00:04:02,270
if react encounters this expense item,
90
00:04:02,270 --> 00:04:05,200
it calls this expense item component function,
91
00:04:05,200 --> 00:04:07,250
executes all the code in there,
92
00:04:07,250 --> 00:04:09,660
encounters this JSX code
93
00:04:09,660 --> 00:04:11,640
and calls this card function
94
00:04:11,640 --> 00:04:14,160
and this expense state function
95
00:04:14,160 --> 00:04:18,410
and then it goes through the JSX code of this components
96
00:04:18,410 --> 00:04:23,250
until there's no more component code left to call.
97
00:04:23,250 --> 00:04:26,290
And then it re-evaluates the overall result
98
00:04:26,290 --> 00:04:29,410
and translates that into DOM instructions
99
00:04:29,410 --> 00:04:32,090
which renders something like this on the screen.
100
00:04:32,090 --> 00:04:33,793
That's how react works.
101
00:04:35,140 --> 00:04:38,200
Now it's all started by the index.js file,
102
00:04:38,200 --> 00:04:41,700
where we initially point at this app component.
103
00:04:41,700 --> 00:04:44,550
That's the first component function which is being called
104
00:04:44,550 --> 00:04:46,700
and that happens when the react app
105
00:04:46,700 --> 00:04:48,410
is been loaded on the screen
106
00:04:48,410 --> 00:04:51,130
which happens when the page is been visited.
107
00:04:51,130 --> 00:04:54,910
So that's how react goes through all these components
108
00:04:54,910 --> 00:04:57,850
executes all these components functions
109
00:04:57,850 --> 00:05:01,030
and draws something on to the screen.
110
00:05:01,030 --> 00:05:03,140
The only problem with that is,
111
00:05:03,140 --> 00:05:06,060
that react never repeats that.
112
00:05:06,060 --> 00:05:08,310
React goes through all of that
113
00:05:08,310 --> 00:05:11,970
when the application is initially rendered,
114
00:05:11,970 --> 00:05:14,350
but thereafter it's done.
115
00:05:14,350 --> 00:05:16,510
However in modern applications,
116
00:05:16,510 --> 00:05:18,730
of course you sometimes want to update
117
00:05:18,730 --> 00:05:20,200
what's visible on the screen,
118
00:05:20,200 --> 00:05:22,470
for example because a button was clicked
119
00:05:22,470 --> 00:05:25,070
and that button should change some text
120
00:05:25,070 --> 00:05:26,980
which is being output.
121
00:05:26,980 --> 00:05:30,020
So we need a way of telling react
122
00:05:30,020 --> 00:05:33,690
that something changed and that a certain component
123
00:05:33,690 --> 00:05:35,910
should be re-evaluated
124
00:05:36,890 --> 00:05:39,820
and that's where react introduces
125
00:05:39,820 --> 00:05:42,353
a special concept called state.
9324
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.