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:04,090
Being able to set styles
2
00:00:04,090 --> 00:00:06,480
and classes dynamically is super important.
3
00:00:06,480 --> 00:00:10,940
And you now learned how you can achieve this in react code.
4
00:00:10,940 --> 00:00:12,580
Now let's move to something else
5
00:00:12,580 --> 00:00:14,610
and that would be the styling itself.
6
00:00:14,610 --> 00:00:19,350
Currently we're using just CSS for that regular CSS files
7
00:00:19,350 --> 00:00:23,060
with regular selectors, mostly class selectors
8
00:00:23,060 --> 00:00:27,140
but also sometimes combined with tag selectors for example.
9
00:00:27,140 --> 00:00:30,700
We're importing these files into our component files,
10
00:00:30,700 --> 00:00:33,630
but as I emphasized multiple times,
11
00:00:33,630 --> 00:00:37,130
this does not result in those styles
12
00:00:37,130 --> 00:00:39,850
being scoped to this component only.
13
00:00:39,850 --> 00:00:43,190
So that means that if we had any other element
14
00:00:43,190 --> 00:00:44,530
anywhere else in a dorm
15
00:00:44,530 --> 00:00:47,640
that had a form controlled CSS class on it,
16
00:00:47,640 --> 00:00:52,640
this style for example, would target it and would affect it.
17
00:00:52,770 --> 00:00:55,270
So it's not like we only care
18
00:00:55,270 --> 00:00:58,800
about foreign control elements in this component, no,
19
00:00:58,800 --> 00:01:01,790
every element on the entire page is affected
20
00:01:01,790 --> 00:01:05,563
because by default, those styles are not scoped.
21
00:01:06,900 --> 00:01:09,560
Now that is not necessarily a problem.
22
00:01:09,560 --> 00:01:13,040
You can of course be careful about your selectors.
23
00:01:13,040 --> 00:01:15,960
You can use class selectors or ID selectors here,
24
00:01:15,960 --> 00:01:19,210
it is regular CSS after all, you can use any selector
25
00:01:19,210 --> 00:01:21,060
and you can ensure that the IDs
26
00:01:21,060 --> 00:01:23,030
and the classes you are selecting
27
00:01:23,030 --> 00:01:25,010
are only used in the places
28
00:01:25,010 --> 00:01:26,963
where you want to apply the styling.
29
00:01:28,050 --> 00:01:30,640
But of course, especially in larger projects,
30
00:01:30,640 --> 00:01:34,250
potentially with a lot of developers working on the code,
31
00:01:34,250 --> 00:01:38,710
it could happen that a name is used twice.
32
00:01:38,710 --> 00:01:40,710
That form control, for example,
33
00:01:40,710 --> 00:01:43,950
is used in different parts of the application
34
00:01:43,950 --> 00:01:45,820
and therefore unfortunately
35
00:01:45,820 --> 00:01:48,520
styles spill over to our components
36
00:01:48,520 --> 00:01:51,400
and you might want to avoid this.
37
00:01:51,400 --> 00:01:53,730
And there are various ways of avoiding this
38
00:01:53,730 --> 00:01:54,850
and I will show you
39
00:01:54,850 --> 00:01:57,233
the two most popular approaches therefore.
40
00:01:58,080 --> 00:02:00,870
Approach number one is to use a package
41
00:02:00,870 --> 00:02:03,480
called styled components.
42
00:02:03,480 --> 00:02:06,110
And you can just search for style components
43
00:02:06,110 --> 00:02:08,023
to find their official webpage.
44
00:02:09,259 --> 00:02:12,560
Now, on this page you can learn all about style components
45
00:02:12,560 --> 00:02:14,950
here I will only introduce you to the package
46
00:02:14,950 --> 00:02:17,330
if you wanna use it you can dive way deeper into it
47
00:02:17,330 --> 00:02:18,740
with those docks
48
00:02:18,740 --> 00:02:22,310
but I will give you a thorough introduction no worries.
49
00:02:22,310 --> 00:02:26,170
Style components is a package that helps you build
50
00:02:26,170 --> 00:02:29,220
guess what, components which have certain styles
51
00:02:29,220 --> 00:02:30,500
attached to them
52
00:02:30,500 --> 00:02:33,640
where the styles really only affect the components
53
00:02:33,640 --> 00:02:36,823
to which they were attached and not any other components.
54
00:02:37,900 --> 00:02:39,040
Now to get started,
55
00:02:39,040 --> 00:02:40,950
we need to install that package.
56
00:02:40,950 --> 00:02:44,190
And here you see the command you need to run for that.
57
00:02:44,190 --> 00:02:46,720
Now, if you've never installed packages before,
58
00:02:46,720 --> 00:02:48,650
keep in mind that our project here
59
00:02:48,650 --> 00:02:51,770
already has a couple of packages on which it depends.
60
00:02:51,770 --> 00:02:53,180
For example, react.
61
00:02:53,180 --> 00:02:56,380
And we're just adding one more package here.
62
00:02:56,380 --> 00:02:58,590
For that you should quit your development server
63
00:02:58,590 --> 00:03:00,370
with Ctrl + C
64
00:03:00,370 --> 00:03:05,120
and run npm_install--save styled-components
65
00:03:05,120 --> 00:03:07,143
inside of this project folder.
66
00:03:08,550 --> 00:03:10,380
Notice we'll download this package
67
00:03:10,380 --> 00:03:12,860
and store it in a node modules folder,
68
00:03:12,860 --> 00:03:16,250
and thereafter you'll be able to use it in this project.
69
00:03:16,250 --> 00:03:18,680
So let's wait for this to finish.
70
00:03:18,680 --> 00:03:20,220
Yep, here it is.
71
00:03:20,220 --> 00:03:21,320
And with this finished
72
00:03:21,320 --> 00:03:23,653
you can restart the server with NPM start.
73
00:03:25,100 --> 00:03:27,420
So now how do we use this package?
74
00:03:27,420 --> 00:03:30,170
Of course you can learn all about it on the official page
75
00:03:30,170 --> 00:03:32,720
but I will simply introduce you to it step by step.
76
00:03:33,640 --> 00:03:35,920
Now let's pick a place where we do wanna use it
77
00:03:35,920 --> 00:03:38,600
and let's say we want to style our button.
78
00:03:38,600 --> 00:03:40,800
We actually already have a button component
79
00:03:40,800 --> 00:03:43,340
but we really only created this button component
80
00:03:43,340 --> 00:03:44,700
if we're honest
81
00:03:44,700 --> 00:03:48,530
because we wanted to have some default styling for a button.
82
00:03:48,530 --> 00:03:50,230
Nothing wrong with this approach,
83
00:03:50,230 --> 00:03:53,210
but here we could also use the stalled components package
84
00:03:53,210 --> 00:03:56,290
to achieve this in a bit of an easier way.
85
00:03:56,290 --> 00:03:58,250
Now, how would we do this?
86
00:03:58,250 --> 00:04:00,650
We could rebuild our button here
87
00:04:00,650 --> 00:04:02,720
by creating a new const Button
88
00:04:02,720 --> 00:04:06,570
and now what we store in there is not a functional component
89
00:04:06,570 --> 00:04:11,570
but instead we now import styled from 'styled-components.'
90
00:04:12,430 --> 00:04:13,800
This import is now available
91
00:04:13,800 --> 00:04:16,089
since you installed the package.
92
00:04:16,089 --> 00:04:18,810
We can get rid of the button CSS import,
93
00:04:18,810 --> 00:04:23,310
and instead now we use styled.button
94
00:04:23,310 --> 00:04:26,293
and then we add two back ticks like this.
95
00:04:27,220 --> 00:04:30,330
Now that certainly is a super weird syntax
96
00:04:30,330 --> 00:04:33,003
and chances are, you haven't seen that before.
97
00:04:33,930 --> 00:04:37,650
This is called attacked template, literal.
98
00:04:37,650 --> 00:04:40,110
It's a default JavaScript feature,
99
00:04:40,110 --> 00:04:44,030
it's not specific to this package, not specific to react
100
00:04:44,030 --> 00:04:47,230
you could use it in any JavaScript project.
101
00:04:47,230 --> 00:04:49,010
And now what does this do?
102
00:04:49,010 --> 00:04:54,010
Well, button is simply a method on this styled object.
103
00:04:54,700 --> 00:04:57,570
Styled is an object we're importing from style components
104
00:04:57,570 --> 00:05:00,300
and there we can access the button method.
105
00:05:00,300 --> 00:05:03,650
It's just a special kind of method you could say.
106
00:05:03,650 --> 00:05:04,950
Instead of being a method,
107
00:05:04,950 --> 00:05:07,580
which you call like this with parentheses,
108
00:05:07,580 --> 00:05:10,310
you add these strange back ticks after it.
109
00:05:10,310 --> 00:05:12,840
And again, that's just a default JavaScript syntax
110
00:05:12,840 --> 00:05:15,190
which is supported by JavaScript.
111
00:05:15,190 --> 00:05:17,320
In the end this will be executed
112
00:05:17,320 --> 00:05:19,060
as a method behind the scenes
113
00:05:19,060 --> 00:05:20,970
and what you pass in here
114
00:05:20,970 --> 00:05:24,560
will be passed into this method just in a special way.
115
00:05:24,560 --> 00:05:25,610
If you wanna learn more
116
00:05:25,610 --> 00:05:28,350
about this tact template literal syntax,
117
00:05:28,350 --> 00:05:31,260
you find a link attached where you can learn more.
118
00:05:31,260 --> 00:05:33,460
Here, I don't wanna waste your time,
119
00:05:33,460 --> 00:05:37,240
since we won't need to know the internals here to proceed.
120
00:05:37,240 --> 00:05:38,600
For the moment we just need to know
121
00:05:38,600 --> 00:05:41,220
that what we pass between these two back ticks
122
00:05:41,220 --> 00:05:44,290
will kind of end up in that button method.
123
00:05:44,290 --> 00:05:47,410
And this button method, and that's the interesting part,
124
00:05:47,410 --> 00:05:50,770
will return a new button component.
125
00:05:50,770 --> 00:05:52,800
Actually, that styled package
126
00:05:52,800 --> 00:05:56,340
has methods for all HTML elements.
127
00:05:56,340 --> 00:06:00,930
It has p method for a paragraph h1, h2 and so on
128
00:06:00,930 --> 00:06:02,950
for the h1, h2 tags,
129
00:06:02,950 --> 00:06:05,610
div to create a div and so on.
130
00:06:05,610 --> 00:06:08,420
It's all there and here we need the button.
131
00:06:08,420 --> 00:06:12,230
So this overall expression will return a button
132
00:06:12,230 --> 00:06:14,130
but a button with the styles
133
00:06:14,130 --> 00:06:16,810
we provide between these two back ticks.
134
00:06:16,810 --> 00:06:18,593
The cool thing about the back tick Syntax is,
135
00:06:18,593 --> 00:06:21,210
that you can write a multiline string like this,
136
00:06:21,210 --> 00:06:23,280
so you can split this across multiple lines
137
00:06:23,280 --> 00:06:25,250
without any extra Syntax.
138
00:06:25,250 --> 00:06:28,660
And now we can just copy all the button styles here
139
00:06:28,660 --> 00:06:30,670
and add them in here.
140
00:06:30,670 --> 00:06:34,190
We'll need to tweak them, but that is something we can do.
141
00:06:34,190 --> 00:06:37,500
Now, what do we need to do to make this work in the end?
142
00:06:37,500 --> 00:06:39,050
This will return a button
143
00:06:39,050 --> 00:06:41,760
and this button won't have a class of button
144
00:06:41,760 --> 00:06:43,940
because we have no place
145
00:06:43,940 --> 00:06:45,943
where we would set up this class name.
146
00:06:47,090 --> 00:06:49,290
So instead, the styles will pass
147
00:06:49,290 --> 00:06:51,550
between the opening and closing back tick
148
00:06:51,550 --> 00:06:53,880
will directly effect this button.
149
00:06:53,880 --> 00:06:56,680
You therefore get rid of any selectors like this
150
00:06:57,800 --> 00:07:00,660
and instead just paste in your styles, just like that.
151
00:07:00,660 --> 00:07:03,200
And they will be added to the button element
152
00:07:03,200 --> 00:07:05,430
which is returned by this method call
153
00:07:05,430 --> 00:07:07,780
which extends toward into this Button constant.
154
00:07:09,300 --> 00:07:13,500
Now for a pseudo selectors, you can use the ampersand symbol
155
00:07:13,500 --> 00:07:16,640
that's supported by that styled component package.
156
00:07:16,640 --> 00:07:20,990
This tells the package that you wanna have a special case
157
00:07:20,990 --> 00:07:23,360
special pseudo selector in this case here
158
00:07:23,360 --> 00:07:26,210
for this component, which is created here.
159
00:07:26,210 --> 00:07:27,127
So in this case, we're saying,
160
00:07:27,127 --> 00:07:30,320
"Hey, when this button here has focus,
161
00:07:30,320 --> 00:07:31,940
please apply to style."
162
00:07:31,940 --> 00:07:33,960
We can do the same here for hover
163
00:07:33,960 --> 00:07:38,493
and :hover and, :active.
164
00:07:40,070 --> 00:07:43,840
And this should now give us a button which has these styles.
165
00:07:43,840 --> 00:07:47,230
Now the button which is returned all to by default
166
00:07:47,230 --> 00:07:49,330
applies all the props
167
00:07:49,330 --> 00:07:52,580
you might be passing to your own button component
168
00:07:52,580 --> 00:07:54,800
which we're exporting here in the end.
169
00:07:54,800 --> 00:07:58,410
So we can still add on click prop, we can still set the type
170
00:07:58,410 --> 00:08:02,630
that will all be forwarded by the style components package,
171
00:08:02,630 --> 00:08:04,320
to that core built in button,
172
00:08:04,320 --> 00:08:07,940
which we previously wrapped or used ourselves
173
00:08:07,940 --> 00:08:11,323
which is now used internally by this button method.
174
00:08:12,620 --> 00:08:14,830
We can now get rid of the react import up there
175
00:08:14,830 --> 00:08:18,120
because we're not dealing with JSX in this file anymore.
176
00:08:18,120 --> 00:08:22,150
And if we now save this, we should see that our application
177
00:08:22,150 --> 00:08:24,960
looks and works exactly as it did before.
178
00:08:24,960 --> 00:08:28,633
So this still works, but this is now our style button.
179
00:08:29,580 --> 00:08:31,610
Now we can tell that there's something different
180
00:08:31,610 --> 00:08:33,823
if we inspect it in the developer tools.
181
00:08:35,159 --> 00:08:36,929
Of course it's a regular button,
182
00:08:36,929 --> 00:08:40,710
that's what this button method here in the end returns.
183
00:08:40,710 --> 00:08:44,440
But you see on that, there is a very strange class
184
00:08:44,440 --> 00:08:47,290
two strange classes to be precise.
185
00:08:47,290 --> 00:08:51,620
And these are class names we certainly didn't set up.
186
00:08:51,620 --> 00:08:54,260
These are class names dynamically generated
187
00:08:54,260 --> 00:08:56,510
by the styled components package.
188
00:08:56,510 --> 00:08:59,140
Because what this package does in the end
189
00:08:59,140 --> 00:09:01,890
is it looks at the styles we set up here
190
00:09:01,890 --> 00:09:06,890
and then it wraps these styles into generated class names
191
00:09:06,970 --> 00:09:09,820
where it guarantees that every class name is unique
192
00:09:09,820 --> 00:09:14,020
so that it can spill over to other components of the app.
193
00:09:14,020 --> 00:09:17,850
And it will then add these classes as global CSS.
194
00:09:17,850 --> 00:09:20,610
But now since we have unique class names
195
00:09:20,610 --> 00:09:24,360
for every styled component, the style set up here
196
00:09:24,360 --> 00:09:25,970
will never be able to affect
197
00:09:25,970 --> 00:09:28,210
the number of component in the app
198
00:09:28,210 --> 00:09:29,970
because these unique class names
199
00:09:29,970 --> 00:09:32,693
are really well unique per style component.
200
00:09:33,550 --> 00:09:35,780
So that's how the styling is applied here
201
00:09:35,780 --> 00:09:38,550
and that's how styled components work.
202
00:09:38,550 --> 00:09:41,870
Now, of course we could also use stall components
203
00:09:41,870 --> 00:09:44,353
when we want to set up something dynamic.
16358
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.