Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,160 --> 00:00:05,090
Fragments allow us to write cleaner code,
2
00:00:05,090 --> 00:00:08,880
to end up with less unnecessary HTML elements
3
00:00:08,880 --> 00:00:09,870
on the final page.
4
00:00:09,870 --> 00:00:10,853
And that's great.
5
00:00:11,920 --> 00:00:14,740
React portals are another useful feature,
6
00:00:14,740 --> 00:00:16,580
which do something similar,
7
00:00:16,580 --> 00:00:19,120
which also help us write cleaner code.
8
00:00:19,120 --> 00:00:21,183
Consider this made up example here.
9
00:00:22,160 --> 00:00:25,130
We got two elements, two components side by side,
10
00:00:25,130 --> 00:00:28,510
MyModal and MyInputForm, wrapped by a Fragment.
11
00:00:28,510 --> 00:00:30,220
But the Fragment won't matter here.
12
00:00:30,220 --> 00:00:32,420
We already learned about that.
13
00:00:32,420 --> 00:00:36,050
And the actual content, that ends up
14
00:00:36,050 --> 00:00:39,223
in the real DOM, might look something like this.
15
00:00:40,230 --> 00:00:42,740
We might have a section and h2 tag,
16
00:00:42,740 --> 00:00:44,439
coming from some other components,
17
00:00:44,439 --> 00:00:46,150
because the code on the left,
18
00:00:46,150 --> 00:00:48,110
is just one of many components
19
00:00:48,110 --> 00:00:49,650
in our application, let's say.
20
00:00:49,650 --> 00:00:52,690
And then we have that brown content,
21
00:00:52,690 --> 00:00:53,990
which is actually the modal,
22
00:00:53,990 --> 00:00:55,710
which is being rendered to the DOM
23
00:00:55,710 --> 00:00:56,890
in the end by React.
24
00:00:56,890 --> 00:00:59,653
And the orange part is our form.
25
00:01:01,240 --> 00:01:03,013
Now what's wrong with that?
26
00:01:04,120 --> 00:01:06,755
Technically nothing, technically this is fine,
27
00:01:06,755 --> 00:01:09,250
but there is something in this code,
28
00:01:09,250 --> 00:01:10,760
which is not ideal.
29
00:01:10,760 --> 00:01:12,943
And that would be that modal.
30
00:01:14,100 --> 00:01:16,980
This modal code, which is being rendered here
31
00:01:16,980 --> 00:01:19,530
in the DOM, will technically work.
32
00:01:19,530 --> 00:01:21,990
As long as you apply the correct styling,
33
00:01:21,990 --> 00:01:23,460
it will look like a modal.
34
00:01:23,460 --> 00:01:26,393
As you also saw in that demo project we worked on.
35
00:01:27,310 --> 00:01:29,880
But semantically and from a,
36
00:01:29,880 --> 00:01:32,930
have a clean HTML structure perspective,
37
00:01:32,930 --> 00:01:34,750
this is not ideal.
38
00:01:34,750 --> 00:01:35,750
Why?
39
00:01:35,750 --> 00:01:37,190
Because you have to keep in mind,
40
00:01:37,190 --> 00:01:39,644
that a modal basically is an overlay
41
00:01:39,644 --> 00:01:41,310
on your page.
42
00:01:41,310 --> 00:01:44,330
It is an overlay to the entire page.
43
00:01:44,330 --> 00:01:47,513
So logically, it's above everything else.
44
00:01:48,350 --> 00:01:52,400
And if it's then nested in some other HTML code,
45
00:01:52,400 --> 00:01:55,290
it might technically work because of styling,
46
00:01:55,290 --> 00:01:57,030
but it's not good code.
47
00:01:57,030 --> 00:01:59,320
It's not a good structure.
48
00:01:59,320 --> 00:02:02,230
And it can even lead to real problems again,
49
00:02:02,230 --> 00:02:05,180
with styling, or with accessibility,
50
00:02:05,180 --> 00:02:08,090
if you have such nested overlay content,
51
00:02:08,090 --> 00:02:11,150
because if, for example, a screen reader
52
00:02:11,150 --> 00:02:14,010
has to interpret your HTML code,
53
00:02:14,010 --> 00:02:15,220
which is being rendered,
54
00:02:15,220 --> 00:02:18,210
it might not see this as a general overlay,
55
00:02:18,210 --> 00:02:21,860
because you are CSS styling won't matter too much there.
56
00:02:21,860 --> 00:02:24,530
And from a semantical perspective,
57
00:02:24,530 --> 00:02:26,860
from the structure it has,
58
00:02:26,860 --> 00:02:30,130
it's deeply structured in our HTML code.
59
00:02:30,130 --> 00:02:32,290
So it's not obvious that this modal
60
00:02:32,290 --> 00:02:35,023
should be an overlay to all the other content.
61
00:02:35,910 --> 00:02:38,100
And that's not just a case for modals,
62
00:02:38,100 --> 00:02:40,870
you would have similar problems for side drawers,
63
00:02:40,870 --> 00:02:43,394
for dialogues, so typically for all kinds
64
00:02:43,394 --> 00:02:47,990
of overlays or any related components.
65
00:02:47,990 --> 00:02:51,440
It's a little bit as if you would create a button,
66
00:02:51,440 --> 00:02:53,870
by simply styling a div like a button
67
00:02:53,870 --> 00:02:56,210
and adding an event listener.
68
00:02:56,210 --> 00:02:59,730
It will work technically, but it's not a good practice.
69
00:02:59,730 --> 00:03:01,920
It's bad for accessibility.
70
00:03:01,920 --> 00:03:03,990
It's bad if a fellow developer has to
71
00:03:03,990 --> 00:03:06,380
work with that, it's simply not a good idea.
72
00:03:06,380 --> 00:03:08,650
In web development in general,
73
00:03:08,650 --> 00:03:11,450
with HTML and CSS and Java script,
74
00:03:11,450 --> 00:03:14,260
because it's so forgiving, you can make a lot
75
00:03:14,260 --> 00:03:17,010
of things work, but that doesn't mean
76
00:03:17,010 --> 00:03:18,704
that just because it works,
77
00:03:18,704 --> 00:03:21,070
it's also a good implementation.
78
00:03:21,070 --> 00:03:23,933
It's also a good idea to use it like this.
79
00:03:24,900 --> 00:03:27,950
And that's where we can use another React concept,
80
00:03:27,950 --> 00:03:30,510
to get rid of this problem with this modal,
81
00:03:30,510 --> 00:03:32,080
with this overlay content,
82
00:03:32,080 --> 00:03:34,562
which shouldn't be deeply nested.
83
00:03:34,562 --> 00:03:37,035
We can use a portal to keep
84
00:03:37,035 --> 00:03:40,260
the structure we have on the left,
85
00:03:40,260 --> 00:03:42,820
so to keep writing our components the way
86
00:03:42,820 --> 00:03:45,470
we want to write them, so that we have
87
00:03:45,470 --> 00:03:47,830
no friction when we wanna pass data around
88
00:03:47,830 --> 00:03:49,000
and so on.
89
00:03:49,000 --> 00:03:51,020
But to still render this differently
90
00:03:51,020 --> 00:03:53,390
in the real DOM and to, for example,
91
00:03:53,390 --> 00:03:57,266
render the modal HTML content somewhere else,
92
00:03:57,266 --> 00:03:59,623
than it would normally go to.
93
00:04:00,560 --> 00:04:03,020
So that on the left, the J as X code rewrite
94
00:04:03,020 --> 00:04:06,075
hasn't changed, but the rendered HTML code
95
00:04:06,075 --> 00:04:09,260
is a bit different from our J as X code.
96
00:04:09,260 --> 00:04:12,830
So the modal there is not next to the forum.
97
00:04:12,830 --> 00:04:16,110
And you can achieve this with React portals
98
00:04:16,110 --> 00:04:17,723
So let's see how portals work.
7293
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.