Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,060 --> 00:00:04,140
Styled components can be very useful,
2
00:00:04,140 --> 00:00:07,640
but can we also use them for our div here for example?
3
00:00:07,640 --> 00:00:10,340
The answer is absolutely.
4
00:00:10,340 --> 00:00:11,930
If we want a styled component
5
00:00:11,930 --> 00:00:13,850
which we really only use in this file,
6
00:00:13,850 --> 00:00:16,210
we can also create it in that file.
7
00:00:16,210 --> 00:00:17,920
We can also create a brand new file
8
00:00:17,920 --> 00:00:19,520
as we did it for the button.
9
00:00:19,520 --> 00:00:21,247
But even though we're only using the button here
10
00:00:21,247 --> 00:00:24,409
in this app, you could argue that a button is a component
11
00:00:24,409 --> 00:00:27,170
you might be using in a lot of other places
12
00:00:27,170 --> 00:00:28,890
of the app as well.
13
00:00:28,890 --> 00:00:30,910
Now for the div, that might also be the case.
14
00:00:30,910 --> 00:00:32,520
Maybe you have other form inputs
15
00:00:32,520 --> 00:00:34,340
in other parts of the app as well.
16
00:00:34,340 --> 00:00:35,560
But to also show you
17
00:00:35,560 --> 00:00:38,720
that you can have multiple components per file
18
00:00:38,720 --> 00:00:40,310
and that this might make sense
19
00:00:40,310 --> 00:00:43,290
if a component is really only getting used in that file,
20
00:00:43,290 --> 00:00:45,530
I will now create a brand new component
21
00:00:45,530 --> 00:00:47,620
in that CourseInput file.
22
00:00:47,620 --> 00:00:50,460
Thus far, we always had one component per file,
23
00:00:50,460 --> 00:00:53,300
and that generally is a good rule to keep.
24
00:00:53,300 --> 00:00:56,060
But if you have a component that is really only getting used
25
00:00:56,060 --> 00:00:58,100
by the other component in that file,
26
00:00:58,100 --> 00:01:01,433
having both components in the same fall can make sense too.
27
00:01:02,390 --> 00:01:06,150
So here I want to have my form control, for example.
28
00:01:06,150 --> 00:01:08,050
That name is ultimately up to you.
29
00:01:08,050 --> 00:01:10,650
But since I'll use it as a component in JSX,
30
00:01:10,650 --> 00:01:12,700
it should start with a capital character.
31
00:01:13,660 --> 00:01:16,910
And here again, I'll use the styled components package.
32
00:01:16,910 --> 00:01:20,223
So we can import style from styled dash components.
33
00:01:21,340 --> 00:01:23,320
And then here we can use styled.
34
00:01:23,320 --> 00:01:25,720
And in this case, styled div,
35
00:01:25,720 --> 00:01:28,470
because I want to replace this div later.
36
00:01:28,470 --> 00:01:30,143
So I want to render a div.
37
00:01:31,250 --> 00:01:33,440
So if I go up, I have my styled div
38
00:01:33,440 --> 00:01:35,550
and now the backticks.
39
00:01:35,550 --> 00:01:39,420
And now in here, I want to have those styles.
40
00:01:39,420 --> 00:01:43,650
So we can grab all those from-controlled styles here,
41
00:01:43,650 --> 00:01:47,483
copy them, and add them here between the backticks.
42
00:01:48,350 --> 00:01:51,320
Now, just as before, get rid of the selectors.
43
00:01:51,320 --> 00:01:53,660
Just have your styles in here.
44
00:01:53,660 --> 00:01:55,850
And if you have something like this,
45
00:01:55,850 --> 00:01:59,900
where you have the class that is on that div,
46
00:01:59,900 --> 00:02:01,010
which you're creating here,
47
00:02:01,010 --> 00:02:03,380
and then you wanna target a nested element,
48
00:02:03,380 --> 00:02:04,790
you do the same as we did
49
00:02:04,790 --> 00:02:07,470
in the button with the pseudo-selectors.
50
00:02:07,470 --> 00:02:10,460
You simply use AND,
51
00:02:10,460 --> 00:02:12,870
and this tells styled components
52
00:02:12,870 --> 00:02:16,340
I'm now talking about labels inside of this div
53
00:02:16,340 --> 00:02:19,550
so that AND always refers back to that component
54
00:02:19,550 --> 00:02:20,990
which is being created.
55
00:02:20,990 --> 00:02:23,180
And if you then wanna have nested selectors
56
00:02:23,180 --> 00:02:25,233
you wanna target, that is how you do it.
57
00:02:26,190 --> 00:02:27,820
So we can do this here,
58
00:02:27,820 --> 00:02:30,820
also for the input, of course.
59
00:02:30,820 --> 00:02:34,260
And also here if we have an input that is focused.
60
00:02:34,260 --> 00:02:38,550
And also here if we wanna check whether we have invalid
61
00:02:38,550 --> 00:02:40,510
on this div.
62
00:02:40,510 --> 00:02:41,713
And also here.
63
00:02:43,290 --> 00:02:46,610
With that, we have our styled component,
64
00:02:46,610 --> 00:02:49,550
our form control, which is this styled div.
65
00:02:49,550 --> 00:02:51,280
And now we can go back here
66
00:02:52,200 --> 00:02:54,930
and now replace this div here
67
00:02:54,930 --> 00:02:57,163
with our form control.
68
00:02:59,290 --> 00:03:02,270
If we saved that, it generally works,
69
00:03:02,270 --> 00:03:04,610
and you will see here on that div
70
00:03:04,610 --> 00:03:07,010
we also have these strange unique classes.
71
00:03:07,010 --> 00:03:09,010
You will also see that these are, of course,
72
00:03:09,010 --> 00:03:11,460
different classes than we have on the button,
73
00:03:11,460 --> 00:03:13,800
because, as I said, you'll have unique classes
74
00:03:13,800 --> 00:03:17,453
per component to ensure that styles never spill over.
75
00:03:18,500 --> 00:03:21,790
But you'll also see that if I submit as an empty state,
76
00:03:21,790 --> 00:03:24,090
we don't get the error state here.
77
00:03:24,090 --> 00:03:25,970
Now, of course, this can't work
78
00:03:25,970 --> 00:03:29,860
because we're no longer setting the invalid class here.
79
00:03:29,860 --> 00:03:33,640
And we need that invalid class here in our rules though.
80
00:03:33,640 --> 00:03:35,393
Here and here.
81
00:03:37,361 --> 00:03:39,830
Now the good thing is that the styled components,
82
00:03:39,830 --> 00:03:42,810
so the components returned by these styled components
83
00:03:42,810 --> 00:03:46,430
functions, forward all props you set on them
84
00:03:46,430 --> 00:03:48,060
to the underlying components,
85
00:03:48,060 --> 00:03:50,350
so to the underlying div here.
86
00:03:50,350 --> 00:03:53,350
So we can still add a class name here.
87
00:03:53,350 --> 00:03:56,350
And here, the value we wanna pass in is either
88
00:03:56,350 --> 00:03:58,720
an empty string or invalid.
89
00:03:58,720 --> 00:04:00,830
We no longer need to set form control
90
00:04:00,830 --> 00:04:03,660
because the styles that were attached by form control
91
00:04:03,660 --> 00:04:05,860
are now already getting attached with this setup
92
00:04:05,860 --> 00:04:06,823
we have up there.
93
00:04:08,160 --> 00:04:11,420
So instead of adjusting to set this to invalid or nothing,
94
00:04:11,420 --> 00:04:14,670
so what we can do here is we can simply check
95
00:04:14,670 --> 00:04:17,320
if not is valid.
96
00:04:17,320 --> 00:04:22,320
And if that's the case, I add invalid as a string here.
97
00:04:22,320 --> 00:04:25,370
And with this syntax, I otherwise add nothing,
98
00:04:25,370 --> 00:04:28,110
which is perfectly fine because I need no other clause,
99
00:04:28,110 --> 00:04:29,240
if it is valid.
100
00:04:29,240 --> 00:04:32,373
But if it is invalid, the invalid class is getting added.
101
00:04:33,450 --> 00:04:37,090
And now with that, we have the same behavior as before here
102
00:04:37,090 --> 00:04:38,223
if we add goals.
103
00:04:39,130 --> 00:04:42,133
That, however, is only one way of doing this.
104
00:04:43,040 --> 00:04:45,920
We can do this, and it's perfectly fine,
105
00:04:45,920 --> 00:04:48,270
but you can also utilize another feature
106
00:04:48,270 --> 00:04:50,470
provided by styled components.
107
00:04:50,470 --> 00:04:53,690
You can also add props to your to your styled components
108
00:04:53,690 --> 00:04:56,730
and utilize those props inside of these backticks,
109
00:04:56,730 --> 00:04:58,290
so inside of your styles
110
00:04:58,290 --> 00:05:01,420
to easily change styles dynamically.
111
00:05:01,420 --> 00:05:02,970
How does this work?
112
00:05:02,970 --> 00:05:05,780
Let's say here on our form control,
113
00:05:05,780 --> 00:05:07,500
when we use it down there,
114
00:05:07,500 --> 00:05:09,470
we want to add an invalid prop,
115
00:05:09,470 --> 00:05:11,970
name is up to you because it's your component,
116
00:05:11,970 --> 00:05:13,950
and this should be true or false.
117
00:05:13,950 --> 00:05:16,350
The value we feed in, therefore, is not isValid.
118
00:05:18,340 --> 00:05:21,730
So if isValid is true, we feed in false
119
00:05:21,730 --> 00:05:24,310
because it's not invalid if isValid is true.
120
00:05:25,379 --> 00:05:27,990
If isValid is false, we feed in true,
121
00:05:27,990 --> 00:05:31,723
because invalid is true if isValid is not true.
122
00:05:32,710 --> 00:05:34,240
So we set invalid to true
123
00:05:34,240 --> 00:05:36,770
if what the user entered was invalid.
124
00:05:36,770 --> 00:05:39,770
And this invalid prop can now be used
125
00:05:39,770 --> 00:05:42,070
between those backticks here.
126
00:05:42,070 --> 00:05:43,510
Now, how do we use it?
127
00:05:43,510 --> 00:05:46,140
Well, we wanted to change the border color
128
00:05:46,140 --> 00:05:48,160
of the input, for example, right?
129
00:05:48,160 --> 00:05:51,470
So we go to the place where we set up the border color.
130
00:05:51,470 --> 00:05:54,270
And now here, since you're between backticks,
131
00:05:54,270 --> 00:05:56,760
you can use this dollar sign, curly brace syntax
132
00:05:56,760 --> 00:05:58,230
you already know.
133
00:05:58,230 --> 00:06:01,580
And here you pass in an arrow function,
134
00:06:01,580 --> 00:06:04,120
which receives props as a parameter,
135
00:06:04,120 --> 00:06:08,000
and then should return the text that should be returned
136
00:06:08,000 --> 00:06:09,623
in this exact position.
137
00:06:10,580 --> 00:06:12,630
This function will be called
138
00:06:12,630 --> 00:06:14,830
by the styled components package;
139
00:06:14,830 --> 00:06:18,040
and for props it will feed in all the props
140
00:06:18,040 --> 00:06:21,563
your component gets, so, in this case, the invalid prop.
141
00:06:23,270 --> 00:06:26,830
So here we can now check if invalid is true.
142
00:06:26,830 --> 00:06:29,870
And if this is the case, we return red as a text;
143
00:06:29,870 --> 00:06:31,970
otherwise, we stick to this grayish color.
144
00:06:32,830 --> 00:06:35,573
And this should, of course, be props.invalid.
145
00:06:36,980 --> 00:06:40,610
So with that, we're having this dynamic way
146
00:06:40,610 --> 00:06:43,160
of changing parts of the styles
147
00:06:43,160 --> 00:06:45,540
based on some props that are passed
148
00:06:45,540 --> 00:06:47,633
to our styled component.
149
00:06:48,970 --> 00:06:49,870
So does this.
150
00:06:49,870 --> 00:06:52,460
Now, we also want to set the background color.
151
00:06:52,460 --> 00:06:56,150
So I'll set background here to a dynamic value.
152
00:06:56,150 --> 00:07:00,890
And just as before, I check props invalid.
153
00:07:00,890 --> 00:07:04,880
And if it is invalid, I set this background to
154
00:07:06,240 --> 00:07:09,853
this light reddish color here we picked earlier,
155
00:07:10,950 --> 00:07:14,273
and otherwise I'll set it to transparent.
156
00:07:16,140 --> 00:07:19,673
And with that, we can remove this style here.
157
00:07:20,620 --> 00:07:23,200
Now we can all already remove the label style,
158
00:07:23,200 --> 00:07:26,050
which was based on the existence of the invalid class
159
00:07:26,050 --> 00:07:27,720
which we no longer add.
160
00:07:27,720 --> 00:07:30,680
And instead go up to the label styling here
161
00:07:30,680 --> 00:07:32,680
and add a color definition.
162
00:07:32,680 --> 00:07:36,540
And just as before, the value we set for color is dynamic.
163
00:07:36,540 --> 00:07:38,680
We provide this function, which will be called
164
00:07:38,680 --> 00:07:40,630
by the styled components package.
165
00:07:40,630 --> 00:07:43,270
And we check if the invalid prop is true.
166
00:07:43,270 --> 00:07:45,320
If it is, we set a color of red;
167
00:07:45,320 --> 00:07:46,963
otherwise, a color of black.
168
00:07:48,470 --> 00:07:50,990
And with this approach, if I save this,
169
00:07:50,990 --> 00:07:55,870
we've got the exact same behavior as before as you see.
170
00:07:55,870 --> 00:07:59,330
But now by utilizing styled components only
171
00:07:59,330 --> 00:08:01,530
and by utilizing this feature
172
00:08:01,530 --> 00:08:04,670
of dynamically changing parts of the styles
173
00:08:04,670 --> 00:08:09,203
based on the props that are set on your styled component.
174
00:08:10,300 --> 00:08:15,050
This sometimes can be hard to get into this way of thinking
175
00:08:15,050 --> 00:08:16,870
but it's always a pretty nice way
176
00:08:16,870 --> 00:08:19,550
of working with components and styles.
177
00:08:19,550 --> 00:08:22,230
Therefore, I wanted to show this to you.
178
00:08:22,230 --> 00:08:25,260
Now, there's one last brief use case which I wanna cover
179
00:08:25,260 --> 00:08:28,823
before I show you a more CSS-ish alternative.
14046
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.