Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,140 --> 00:00:04,090
Which alternative could we have
2
00:00:04,090 --> 00:00:06,100
to setting inline styles?
3
00:00:06,100 --> 00:00:08,900
Well, how about the following alternative?
4
00:00:08,900 --> 00:00:13,320
What if we could add a new CSS class to this div
5
00:00:13,320 --> 00:00:15,740
which holds both the label and the input.
6
00:00:15,740 --> 00:00:17,930
And we would add this class dynamically
7
00:00:17,930 --> 00:00:21,750
only if the value entered is invalid.
8
00:00:21,750 --> 00:00:24,730
That could be something like a invalid class here.
9
00:00:24,730 --> 00:00:26,440
The name is totally up to you.
10
00:00:26,440 --> 00:00:30,320
So this invalid class should sometimes be added here
11
00:00:30,320 --> 00:00:32,860
on this div and then in CSS
12
00:00:32,860 --> 00:00:35,140
we could prepare the fitting styles.
13
00:00:35,140 --> 00:00:39,230
For example, here, we could say if the form control also has
14
00:00:39,230 --> 00:00:42,260
the invalid class, and therefore make sure there is no space
15
00:00:42,260 --> 00:00:44,210
between form control and this dot,
16
00:00:44,210 --> 00:00:47,293
which means they both have to be on the same element,
17
00:00:48,440 --> 00:00:51,680
then we could target our input
18
00:00:51,680 --> 00:00:54,343
and set the border color to red.
19
00:00:55,760 --> 00:00:58,190
And we could set the background to red
20
00:00:58,190 --> 00:01:00,980
or actually now pick a much lighter version,
21
00:01:00,980 --> 00:01:03,803
like this very light rose color here.
22
00:01:05,880 --> 00:01:10,350
And if the form control, the div with the class
23
00:01:10,350 --> 00:01:13,280
form control has a class of invalid,
24
00:01:13,280 --> 00:01:17,743
we can also target the label and set its color to red.
25
00:01:18,640 --> 00:01:21,900
So these here would be two CSS rules
26
00:01:21,900 --> 00:01:24,470
that should lead to the desired output.
27
00:01:24,470 --> 00:01:28,330
And all we need to do for these rules to have an effect
28
00:01:28,330 --> 00:01:31,853
is we need to add that invalid class dynamically.
29
00:01:33,090 --> 00:01:35,400
And here's how you can add classes dynamically.
30
00:01:35,400 --> 00:01:37,030
Let's get rid of these inline styles.
31
00:01:37,030 --> 00:01:38,380
We don't need them anymore.
32
00:01:39,250 --> 00:01:40,370
And instead let's see how
33
00:01:40,370 --> 00:01:42,290
we can change this class dynamically.
34
00:01:42,290 --> 00:01:43,780
It's very, very easy.
35
00:01:43,780 --> 00:01:48,290
Since we need a dynamic value, we need curly braces
36
00:01:48,290 --> 00:01:49,860
because a string is always a string.
37
00:01:49,860 --> 00:01:53,250
It's a hard coded string, so we need curly braces.
38
00:01:53,250 --> 00:01:55,500
And now what we feed in, of course,
39
00:01:55,500 --> 00:01:58,670
still can be a string of form control.
40
00:01:58,670 --> 00:02:02,220
But a string where we dynamically add more text in it,
41
00:02:02,220 --> 00:02:03,980
where we dynamically expand it,
42
00:02:03,980 --> 00:02:07,550
or concatenate a value to have this string,
43
00:02:07,550 --> 00:02:11,009
whoops invalid, instead of this string.
44
00:02:11,009 --> 00:02:13,450
So what we pass to class name on this div
45
00:02:13,450 --> 00:02:16,180
sometimes is a string that looks like this,
46
00:02:16,180 --> 00:02:18,670
and sometimes will be a string that looks like this,
47
00:02:18,670 --> 00:02:20,480
depending on what the user entered
48
00:02:20,480 --> 00:02:22,913
and depending on our is valid state.
49
00:02:24,120 --> 00:02:26,410
And for that, we can use a nice construct.
50
00:02:26,410 --> 00:02:28,910
We can use backticks here.
51
00:02:28,910 --> 00:02:31,300
Now, in case you're not familiar with this,
52
00:02:31,300 --> 00:02:33,590
this is a default JavaScript feature.
53
00:02:33,590 --> 00:02:36,400
It's called a template literal.
54
00:02:36,400 --> 00:02:40,170
This constructs a string but we have backticks here,
55
00:02:40,170 --> 00:02:42,530
not single quotes, but backticks.
56
00:02:42,530 --> 00:02:45,000
It's a special character on your keyboard
57
00:02:45,000 --> 00:02:47,290
and whatever you type between there
58
00:02:47,290 --> 00:02:49,100
will be treated as a regular string.
59
00:02:49,100 --> 00:02:52,410
So we can add form control here.
60
00:02:52,410 --> 00:02:54,450
But, and that's not a tricky thing,
61
00:02:54,450 --> 00:02:58,740
you can also inject a dynamic value into that string
62
00:02:58,740 --> 00:03:01,310
with a special syntax, which looks like this,
63
00:03:01,310 --> 00:03:04,340
dollar sign, and then again, a pair of curly braces
64
00:03:04,340 --> 00:03:05,403
opening and closing.
65
00:03:06,310 --> 00:03:09,170
This adds content into the string and what you pass
66
00:03:09,170 --> 00:03:13,440
between these curly braces can be any JavaScript expression.
67
00:03:13,440 --> 00:03:17,180
So a lot like our curly braces inside of JSX.
68
00:03:17,180 --> 00:03:21,010
Here, now inside of the template literal.
69
00:03:21,010 --> 00:03:25,000
So here we could now check if isValid is not true,
70
00:03:25,000 --> 00:03:27,580
hence the exclamation mark at the beginning,
71
00:03:27,580 --> 00:03:28,980
and if that's the case,
72
00:03:28,980 --> 00:03:33,430
we insert invalid here into this long string.
73
00:03:33,430 --> 00:03:34,630
Otherwise we enter nothing
74
00:03:34,630 --> 00:03:37,330
or simply an empty string in this case.
75
00:03:37,330 --> 00:03:41,300
So that simply will make sure that class name is either set
76
00:03:41,300 --> 00:03:45,420
to a string which has only form control, blank nothing,
77
00:03:45,420 --> 00:03:49,163
or to a string, which is form control blank invalid.
78
00:03:50,360 --> 00:03:52,260
So there for now, if we save this,
79
00:03:52,260 --> 00:03:56,470
if I submit this in an empty state, I get my error,
80
00:03:56,470 --> 00:03:58,623
look here, my error style.
81
00:04:00,780 --> 00:04:02,970
But other than that, if I start typing
82
00:04:02,970 --> 00:04:05,690
I go back to the non error style
83
00:04:05,690 --> 00:04:10,210
because we dynamically add this invalid class.
84
00:04:10,210 --> 00:04:12,230
And of course you can inject multiple things
85
00:04:12,230 --> 00:04:13,140
into that string.
86
00:04:13,140 --> 00:04:15,160
So you could repeat the same technique
87
00:04:15,160 --> 00:04:18,190
to add more dynamically added classes.
88
00:04:18,190 --> 00:04:20,920
And that's very powerful because now you're back
89
00:04:20,920 --> 00:04:24,300
to working with CSS files and with classes only
90
00:04:24,300 --> 00:04:27,420
and you can dynamically add or remove classes
91
00:04:27,420 --> 00:04:30,230
with this simple syntax.
92
00:04:30,230 --> 00:04:33,160
And it will be Reacts job to actually add
93
00:04:33,160 --> 00:04:35,200
or remove the classes in the DOM.
94
00:04:35,200 --> 00:04:37,780
You do what you can do best in React.
95
00:04:37,780 --> 00:04:39,480
You specify a goal.
96
00:04:39,480 --> 00:04:42,440
You specify some alternative states, for example,
97
00:04:42,440 --> 00:04:45,420
that the goal sometimes is to have just form control
98
00:04:45,420 --> 00:04:48,030
and sometimes form control and invalid,
99
00:04:48,030 --> 00:04:50,610
and React will make sure that the DOM,
100
00:04:50,610 --> 00:04:54,530
the real DOM, is updated accordingly.
101
00:04:54,530 --> 00:04:56,320
This is React in action
102
00:04:56,320 --> 00:04:58,793
and this is how you can add styles dynamically.
8055
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.