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:03,030
Now we know how
2
00:00:03,030 --> 00:00:06,150
we can generally build class-based components
3
00:00:06,150 --> 00:00:09,720
and how we can manage state and work with events, steer.
4
00:00:09,720 --> 00:00:12,020
But what about side effects?
5
00:00:12,020 --> 00:00:15,520
As I mentioned, you can't use these react hooks
6
00:00:15,520 --> 00:00:19,910
in class-based components, so you can't use use effect
7
00:00:19,910 --> 00:00:22,840
but class-based components have a concept
8
00:00:22,840 --> 00:00:26,230
called the component life cycle.
9
00:00:26,230 --> 00:00:29,730
And of course, every component technically has a life cycle.
10
00:00:29,730 --> 00:00:32,400
It, for example, is rendered to the DOM
11
00:00:32,400 --> 00:00:36,270
or remove from the DOM, but there are specific methods
12
00:00:36,270 --> 00:00:39,810
you can add two class-based components to run code
13
00:00:39,810 --> 00:00:43,230
at different points in that life cycle.
14
00:00:43,230 --> 00:00:47,200
And the first, most important life cycle method you can add
15
00:00:47,200 --> 00:00:48,960
to class-based components,
16
00:00:48,960 --> 00:00:51,450
is to componentDidMount() method
17
00:00:52,330 --> 00:00:55,400
like render() that's a built in method,
18
00:00:55,400 --> 00:00:57,390
which you can use as soon
19
00:00:57,390 --> 00:00:59,960
as you extend the built in component
20
00:00:59,960 --> 00:01:01,570
which you import from React.
21
00:01:01,570 --> 00:01:04,239
And then you can add this method and React.
22
00:01:04,239 --> 00:01:08,970
We'll call it for you when the component was just mounted
23
00:01:08,970 --> 00:01:10,573
but more on that in a second.
24
00:01:11,470 --> 00:01:13,130
Other live cycle
25
00:01:13,130 --> 00:01:14,770
methods you can add would be
26
00:01:14,770 --> 00:01:19,280
componentDidUpdate() and componentWillUnmount().
27
00:01:19,280 --> 00:01:22,437
There are a couple of other less important
28
00:01:22,437 --> 00:01:25,850
to lifecycle methods, which will not take a closer look
29
00:01:25,850 --> 00:01:29,130
at here because these free lifecycle methods
30
00:01:29,130 --> 00:01:33,130
are by far the most important and common ones.
31
00:01:33,130 --> 00:01:34,939
Now, as I said, componentDidMount()
32
00:01:34,939 --> 00:01:37,650
is called when a component is mounted.
33
00:01:37,650 --> 00:01:42,030
So when it was evaluated and now rendered to the DOM.
34
00:01:42,030 --> 00:01:47,030
It's basically the equivalent to using use EFFECT like this.
35
00:01:47,660 --> 00:01:50,300
You have your EFFECT function
36
00:01:50,300 --> 00:01:53,100
which I left out here with the free dots.
37
00:01:53,100 --> 00:01:57,230
And then you have the array of dependencies
38
00:01:57,230 --> 00:02:00,980
and every EFFECT function is executed when
39
00:02:00,980 --> 00:02:03,870
the component first is mounted.
40
00:02:03,870 --> 00:02:05,860
But if you have no dependencies
41
00:02:05,860 --> 00:02:08,270
if you have an empty dependencies array
42
00:02:08,270 --> 00:02:13,260
the EFFECT function is only executed in that case.
43
00:02:13,260 --> 00:02:16,750
So adding use of fact with an empty dependency
44
00:02:16,750 --> 00:02:21,247
array is basically your equivalent to componentDidMount().
45
00:02:22,250 --> 00:02:24,820
And we're going to See componentDidMount()
46
00:02:24,820 --> 00:02:26,453
in action in just a second.
47
00:02:27,360 --> 00:02:30,810
You'll also have componentDidUpdate().
48
00:02:30,810 --> 00:02:35,200
This is called once a component was updated.
49
00:02:35,200 --> 00:02:38,460
So when something changed when your state changed
50
00:02:38,460 --> 00:02:41,940
and the component was re-evaluated and re-rendered
51
00:02:42,890 --> 00:02:46,680
this is basically your equivalent to use effect
52
00:02:46,680 --> 00:02:51,430
with some dependencies, whenever the dependencies change,
53
00:02:51,430 --> 00:02:54,640
the use EFFECT function re-executes
54
00:02:54,640 --> 00:02:57,730
and the dependencies are re-evaluated
55
00:02:57,730 --> 00:03:00,070
when the component is re-evaluated
56
00:03:00,070 --> 00:03:01,890
and was rendered again.
57
00:03:01,890 --> 00:03:02,830
Because you'll learn
58
00:03:02,830 --> 00:03:06,720
that the effects are generally re-executed whenever
59
00:03:06,720 --> 00:03:10,230
a component re-rendered, if dependencies changed.
60
00:03:10,230 --> 00:03:13,717
So that's why this is basically your equivalent to
61
00:03:13,717 --> 00:03:15,710
componentDidDpdate().
62
00:03:15,710 --> 00:03:18,660
Now componentWillUnmount is called right
63
00:03:18,660 --> 00:03:21,780
before the component is removed from the DOM,
64
00:03:21,780 --> 00:03:26,463
and here the equivalent is basically the clean up function
65
00:03:26,463 --> 00:03:31,050
from use EFFECT that cleanup function is called right
66
00:03:31,050 --> 00:03:34,000
before the EFFECT function is executed again
67
00:03:34,000 --> 00:03:37,260
and also always again, when the component
68
00:03:37,260 --> 00:03:40,023
is about to be removed from the DOM.
69
00:03:40,910 --> 00:03:45,170
Now again, let's take a closer look at that in action here.
70
00:03:45,170 --> 00:03:48,700
And for this, I altered the project slightly,
71
00:03:48,700 --> 00:03:51,550
attached you find the UserFinder component,
72
00:03:51,550 --> 00:03:53,630
which you can add to your components.
73
00:03:53,630 --> 00:03:56,810
And now we need to go to the users component
74
00:03:56,810 --> 00:04:00,970
and dare get rid of the dummy users here.
75
00:04:00,970 --> 00:04:04,410
And instead here, where we used the dummy users
76
00:04:04,410 --> 00:04:09,410
refer to this props users, because users are now passed in
77
00:04:11,010 --> 00:04:15,370
through props into this users component.
78
00:04:15,370 --> 00:04:16,640
And now we just need to go
79
00:04:16,640 --> 00:04:21,640
to the App component and use the UserFinder component
80
00:04:22,430 --> 00:04:26,710
and import user finder from components UserFinder.
81
00:04:26,710 --> 00:04:28,710
Instead of users.
82
00:04:28,710 --> 00:04:33,140
also add the UserFinder. Module.CSS file,
83
00:04:33,140 --> 00:04:34,803
which you also find attached.
84
00:04:35,750 --> 00:04:37,460
And if you added all of that
85
00:04:37,460 --> 00:04:40,010
and you saved as you should see an application that looks
86
00:04:40,010 --> 00:04:44,880
like this, and here, if you type you now also
87
00:04:44,880 --> 00:04:48,530
filter this list down there with every keystroke.
88
00:04:48,530 --> 00:04:51,093
It's a very simple filter logic, which I added here
89
00:04:51,093 --> 00:04:54,580
into UserFinder, and the UserFinder component
90
00:04:54,580 --> 00:04:58,900
is a functional component using multiple state slices
91
00:04:58,900 --> 00:05:02,250
and use EFFECT to manage the users
92
00:05:02,250 --> 00:05:04,310
and to then filter the users
93
00:05:04,310 --> 00:05:07,833
and pass the filtered users to the users component.
94
00:05:08,790 --> 00:05:11,070
Now let's transform this component
95
00:05:11,070 --> 00:05:14,080
to a class-based component to get an idea
96
00:05:14,080 --> 00:05:18,503
on how these class-based components deal with side effects.
7740
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.