Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,320 --> 00:00:03,153
Obviously,
2
00:00:03,153 --> 00:00:06,210
state is a crucial concept in React.
3
00:00:06,210 --> 00:00:09,100
Ultimately, everything comes down to state
4
00:00:09,100 --> 00:00:11,240
when it comes to re-rendering components
5
00:00:11,240 --> 00:00:13,420
and changing what's on the screen.
6
00:00:13,420 --> 00:00:17,220
So therefore components and their interaction with state,
7
00:00:17,220 --> 00:00:20,640
that is really a core aspect of React.
8
00:00:20,640 --> 00:00:22,210
And it is worth noting
9
00:00:22,210 --> 00:00:25,800
that both, of course, is managed by React.
10
00:00:25,800 --> 00:00:29,430
This component's concept comes from the React library,
11
00:00:29,430 --> 00:00:32,549
and React also takes care of your state
12
00:00:32,549 --> 00:00:34,700
that is attached to components.
13
00:00:34,700 --> 00:00:37,890
It takes care, for example, by using the useState Hook,
14
00:00:37,890 --> 00:00:39,860
and that's how this interaction
15
00:00:39,860 --> 00:00:43,360
between components and state is handled.
16
00:00:43,360 --> 00:00:44,570
Speaking of that,
17
00:00:44,570 --> 00:00:47,650
one of the most common forms of managing state
18
00:00:47,650 --> 00:00:50,260
is to use the useState Hook.
19
00:00:50,260 --> 00:00:54,290
With that, you create a new piece of state, you could say,
20
00:00:54,290 --> 00:00:57,070
and you automatically attach it to a component.
21
00:00:57,070 --> 00:00:59,820
The component in which you call useState.
22
00:00:59,820 --> 00:01:02,070
This attachment, kind of,
23
00:01:02,070 --> 00:01:04,400
is done behind the scenes by React.
24
00:01:04,400 --> 00:01:06,270
Because when you call useState,
25
00:01:06,270 --> 00:01:10,230
React behind the scenes creates a new state variable,
26
00:01:10,230 --> 00:01:13,070
you could say, which React manages for you,
27
00:01:13,070 --> 00:01:17,750
and which React also for you ties to this component.
28
00:01:17,750 --> 00:01:20,200
Now, I mentioned at the end of last lecture,
29
00:01:20,200 --> 00:01:24,030
that it's strange that even though we call useState
30
00:01:24,030 --> 00:01:26,500
every time the app function runs again,
31
00:01:26,500 --> 00:01:29,420
we don't seem to lose or reinitialize
32
00:01:29,420 --> 00:01:31,050
our state all the time.
33
00:01:31,050 --> 00:01:32,400
The reason for that is simple.
34
00:01:32,400 --> 00:01:35,180
UseState is coming from React,
35
00:01:35,180 --> 00:01:38,000
and I just said that React is managing the state
36
00:01:38,000 --> 00:01:40,940
and the connection to the component for you.
37
00:01:40,940 --> 00:01:44,130
And as part of that management process,
38
00:01:44,130 --> 00:01:47,230
React makes sure that useState
39
00:01:47,230 --> 00:01:51,640
and the value you pass as a default value to useState
40
00:01:51,640 --> 00:01:55,130
essentially is only considered once.
41
00:01:55,130 --> 00:01:58,150
The first time ever a component is rendered,
42
00:01:58,150 --> 00:02:00,480
so the very first time the app component runs,
43
00:02:00,480 --> 00:02:04,960
useState, when executed, creates a new state variable
44
00:02:04,960 --> 00:02:06,480
which is handed off to React
45
00:02:06,480 --> 00:02:08,820
and which is managed by React.
46
00:02:08,820 --> 00:02:11,030
React then basically memorizes
47
00:02:11,030 --> 00:02:13,070
to which component that belongs.
48
00:02:13,070 --> 00:02:15,340
The app component, in our case.
49
00:02:15,340 --> 00:02:16,960
And it uses the default value
50
00:02:16,960 --> 00:02:19,600
to initialize the state with that value.
51
00:02:19,600 --> 00:02:23,160
For subsequent app function calls,
52
00:02:23,160 --> 00:02:26,850
so for reevaluations of the app component,
53
00:02:26,850 --> 00:02:28,960
when useState is being called,
54
00:02:28,960 --> 00:02:31,010
no new state is being created.
55
00:02:31,010 --> 00:02:33,080
Instead, React recognizes
56
00:02:33,080 --> 00:02:36,980
that it already has a state for this component,
57
00:02:36,980 --> 00:02:41,260
and it instead simply updates that state as needed
58
00:02:41,260 --> 00:02:43,240
because the app function reran
59
00:02:43,240 --> 00:02:45,610
because some state changed most likely,
60
00:02:45,610 --> 00:02:47,340
and therefore React will only do
61
00:02:47,340 --> 00:02:49,510
that state management and updating.
62
00:02:49,510 --> 00:02:52,190
It will never reinitialize the state
63
00:02:52,190 --> 00:02:54,890
unless the component was totally removed
64
00:02:54,890 --> 00:02:56,530
from the DOM in the meantime.
65
00:02:56,530 --> 00:02:58,880
Which in case of the app component, will never happen
66
00:02:58,880 --> 00:03:00,710
since it's our root component.
67
00:03:00,710 --> 00:03:01,930
For child components,
68
00:03:01,930 --> 00:03:04,230
if it's rendered conditionally, for example,
69
00:03:04,230 --> 00:03:06,080
a component might be removed,
70
00:03:06,080 --> 00:03:09,260
and in that case, if it's then later reattached,
71
00:03:09,260 --> 00:03:11,240
a new state would be initialized.
72
00:03:11,240 --> 00:03:14,510
But as long as a component stays attached to the DOM,
73
00:03:14,510 --> 00:03:19,120
state is only updated after that first initialization.
74
00:03:19,120 --> 00:03:21,240
That's one important note to take.
75
00:03:21,240 --> 00:03:25,410
And of course the same is true for useReducer, for example.
76
00:03:25,410 --> 00:03:28,210
Now that's not all you need to know about state, though.
5882
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.