Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,020 --> 00:00:05,620
Now let's start with creating the code for the overlay.
2
00:00:05,620 --> 00:00:08,010
For this, I'll add a new component.
3
00:00:08,010 --> 00:00:11,000
We could add the code in the existing component
4
00:00:11,000 --> 00:00:13,570
but it is considered a good practice
5
00:00:13,570 --> 00:00:17,050
to put every building block into its own file,
6
00:00:17,050 --> 00:00:19,400
every component into its file,
7
00:00:19,400 --> 00:00:22,620
and to avoid very long JSX blocks
8
00:00:22,620 --> 00:00:26,160
by instead splitting it up into separate components.
9
00:00:26,160 --> 00:00:28,450
Now, when you should make a split,
10
00:00:28,450 --> 00:00:30,900
that's something which will come with experience.
11
00:00:30,900 --> 00:00:32,580
And you'll see a couple of examples
12
00:00:32,580 --> 00:00:34,480
throughout this course here.
13
00:00:34,480 --> 00:00:37,760
But in general, you wanna keep your component functions
14
00:00:37,760 --> 00:00:39,660
small and maintainable.
15
00:00:39,660 --> 00:00:42,730
The JSX code that belongs to a component
16
00:00:42,730 --> 00:00:45,060
shouldn't become too complex.
17
00:00:45,060 --> 00:00:48,340
And therefore I'll add a new file in the components folder
18
00:00:48,340 --> 00:00:50,740
and I'll name it modal JS
19
00:00:50,740 --> 00:00:53,780
because that's the component that should be responsible
20
00:00:53,780 --> 00:00:56,610
for rendering a modal, this overlay.
21
00:00:56,610 --> 00:00:59,220
That's typically called a modal.
22
00:00:59,220 --> 00:01:01,820
And then here we therefore create another function,
23
00:01:01,820 --> 00:01:03,350
the modal function,
24
00:01:03,350 --> 00:01:07,950
and we export this function, like this.
25
00:01:07,950 --> 00:01:11,350
And here we can also accept this props parameter.
26
00:01:11,350 --> 00:01:12,380
As I mentioned,
27
00:01:12,380 --> 00:01:15,110
every component function gets this parameter.
28
00:01:15,110 --> 00:01:18,220
It's passed in automatically by React.
29
00:01:18,220 --> 00:01:20,660
But of course, if you don't use this parameter
30
00:01:20,660 --> 00:01:21,493
in the function,
31
00:01:21,493 --> 00:01:23,560
if you don't use it in a given component,
32
00:01:23,560 --> 00:01:25,120
you can also omit it.
33
00:01:25,120 --> 00:01:27,343
And here for the moment, I will omit it.
34
00:01:28,290 --> 00:01:30,480
Then we need to return something here,
35
00:01:30,480 --> 00:01:31,970
something which can be rendered
36
00:01:31,970 --> 00:01:33,420
by the browser as we learned.
37
00:01:33,420 --> 00:01:36,330
And for me, that will be another div.
38
00:01:36,330 --> 00:01:38,730
And in that div, we can then have a paragraph,
39
00:01:38,730 --> 00:01:42,173
let's say, with a text of, are you sure?
40
00:01:43,030 --> 00:01:44,840
Or something like this.
41
00:01:44,840 --> 00:01:48,000
And below that, let's say two buttons.
42
00:01:48,000 --> 00:01:50,750
One button where we say cancel.
43
00:01:50,750 --> 00:01:53,860
And one button where we say confirm.
44
00:01:53,860 --> 00:01:57,200
Now in this demo, the buttons won't really do anything
45
00:01:57,200 --> 00:01:59,860
except for closing the modal.
46
00:01:59,860 --> 00:02:01,580
But of course, in a bigger application,
47
00:02:01,580 --> 00:02:04,100
you could then trigger different actions
48
00:02:04,100 --> 00:02:06,760
based on which button was clicked.
49
00:02:06,760 --> 00:02:08,639
But again, that's just a first demo.
50
00:02:08,639 --> 00:02:11,000
We're going to see a slightly bigger app
51
00:02:11,000 --> 00:02:13,120
thereafter in this course.
52
00:02:13,120 --> 00:02:14,790
So for the moment, it's this div.
53
00:02:14,790 --> 00:02:19,130
Let me format this with a paragraph and two buttons.
54
00:02:19,130 --> 00:02:20,780
Now, we want to add some styling.
55
00:02:20,780 --> 00:02:23,330
So on that div, I'll add a class name,
56
00:02:23,330 --> 00:02:25,490
as you learned, of modal.
57
00:02:25,490 --> 00:02:28,440
And on the buttons, I'll also add some class names.
58
00:02:28,440 --> 00:02:30,340
On the cancel button, it's button.
59
00:02:30,340 --> 00:02:34,410
And then another class named button dash dash alt.
60
00:02:34,410 --> 00:02:38,270
And these are simply classes to find in the index CSS file
61
00:02:38,270 --> 00:02:41,050
I provided to you earlier.
62
00:02:41,050 --> 00:02:44,363
And then here on the second button, it's just a button.
63
00:02:45,260 --> 00:02:49,283
And that's now the markup for this overlay component.
64
00:02:51,070 --> 00:02:53,050
Now, in addition to this overlay,
65
00:02:53,050 --> 00:02:55,000
we also wanna have a backdrop.
66
00:02:55,000 --> 00:02:58,100
Some gray background behind the modal
67
00:02:58,100 --> 00:03:00,590
which blocks interaction with the page
68
00:03:00,590 --> 00:03:03,030
whilst the modal is opened.
69
00:03:03,030 --> 00:03:05,430
And for this, I'll add another component,
70
00:03:05,430 --> 00:03:09,480
the backdrop component in a backdrop JS file.
71
00:03:09,480 --> 00:03:13,350
And that will also be a straightforward component,
72
00:03:13,350 --> 00:03:16,893
simply a backdrop function here like this.
73
00:03:18,280 --> 00:03:21,273
And then we export this as a default.
74
00:03:22,140 --> 00:03:24,960
And here I just want to return a div
75
00:03:24,960 --> 00:03:27,770
with a class name of backdrop.
76
00:03:27,770 --> 00:03:29,870
This div has no content
77
00:03:29,870 --> 00:03:31,270
and therefore we can write it
78
00:03:31,270 --> 00:03:33,750
as a self-closing tag actually.
79
00:03:33,750 --> 00:03:38,070
In standard HTML, you would not write it like this.
80
00:03:38,070 --> 00:03:41,470
There divs are not void tags,
81
00:03:41,470 --> 00:03:44,100
but in JSX you are allowed to do that
82
00:03:44,100 --> 00:03:46,180
if you have no actual content
83
00:03:46,180 --> 00:03:48,490
between the opening and closing tags.
84
00:03:48,490 --> 00:03:50,360
And here I just rendered this div
85
00:03:50,360 --> 00:03:53,520
because of this styling which is added by this class.
86
00:03:53,520 --> 00:03:55,260
And hence I need no content
87
00:03:55,260 --> 00:03:58,053
and hence we can write it like a self-closing tag.
88
00:03:59,180 --> 00:04:02,610
Okay, so now we've got the backdrop and the modal.
89
00:04:02,610 --> 00:04:04,040
Now we can use both.
90
00:04:04,040 --> 00:04:07,330
And for the moment, we will always show both.
91
00:04:07,330 --> 00:04:10,470
So for the moment, we can go to app JS, for example,
92
00:04:10,470 --> 00:04:12,840
and below all these To do's,
93
00:04:12,840 --> 00:04:15,250
we can now also output the modal
94
00:04:15,250 --> 00:04:17,490
as a self-closing tag like this
95
00:04:17,490 --> 00:04:21,500
and the backdrop as a self-closing tag like this.
96
00:04:21,500 --> 00:04:24,950
And we again need to import these two component functions
97
00:04:24,950 --> 00:04:26,810
in order to use them here.
98
00:04:26,810 --> 00:04:30,720
So we import modal from dot slash components modal
99
00:04:31,990 --> 00:04:34,050
and we import backdrop
100
00:04:34,050 --> 00:04:37,793
from dot slash components backdrop, like this.
101
00:04:39,190 --> 00:04:41,990
If you now save all your files,
102
00:04:41,990 --> 00:04:44,260
you should see something like this.
103
00:04:44,260 --> 00:04:45,490
Let me zoom out a bit.
104
00:04:45,490 --> 00:04:46,930
Something like this.
105
00:04:46,930 --> 00:04:48,440
This is our modal.
106
00:04:48,440 --> 00:04:50,650
And at the moment, it's always visible
107
00:04:50,650 --> 00:04:52,010
and we can't close it
108
00:04:52,860 --> 00:04:55,880
because that's now one big missing piece.
109
00:04:55,880 --> 00:04:58,010
We know how to build components,
110
00:04:58,010 --> 00:05:00,260
how to build reusable components,
111
00:05:00,260 --> 00:05:02,710
and how to build our interface.
112
00:05:02,710 --> 00:05:07,470
But at this point, our interface is 100% static.
113
00:05:07,470 --> 00:05:08,830
It never changes.
114
00:05:08,830 --> 00:05:11,890
What we output here, that never changes.
115
00:05:11,890 --> 00:05:15,200
No matter how we interact with it as a user,
116
00:05:15,200 --> 00:05:18,170
the interface always stays the same.
117
00:05:18,170 --> 00:05:20,680
And of course, that's typically not what you want
118
00:05:20,680 --> 00:05:23,040
in a web interface.
119
00:05:23,040 --> 00:05:26,600
Things should be interactive and things should change.
120
00:05:26,600 --> 00:05:28,080
That's one of the main reasons
121
00:05:28,080 --> 00:05:31,100
for using a library like React.
122
00:05:31,100 --> 00:05:32,780
And therefore let's now explore
123
00:05:32,780 --> 00:05:35,530
how React makes sure that we can change
124
00:05:35,530 --> 00:05:38,833
what we see after the page was loaded.
9441
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.