Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,080 --> 00:00:05,570
Let's have a closer look at the JSX.
2
00:00:05,570 --> 00:00:08,920
Now I mentioned before, earlier in this module,
3
00:00:08,920 --> 00:00:11,020
you can open up your developer tools
4
00:00:11,020 --> 00:00:13,570
and there you'll find the JavaScript code
5
00:00:13,570 --> 00:00:17,340
which is responsible for what you're seeing on the page.
6
00:00:17,340 --> 00:00:19,610
And in there, you will also find
7
00:00:19,610 --> 00:00:21,990
all your component functions,
8
00:00:21,990 --> 00:00:23,960
like this app function here.
9
00:00:23,960 --> 00:00:26,130
Now it will look totally different in the end,
10
00:00:26,130 --> 00:00:28,640
because there you have no JSX code,
11
00:00:28,640 --> 00:00:32,170
because JSX code isn't supported by the browser.
12
00:00:32,170 --> 00:00:36,060
This, in the end, is just syntactic sugar, which works here
13
00:00:36,060 --> 00:00:39,000
thanks to the behind-the-scenes transformations.
14
00:00:39,000 --> 00:00:40,500
Now we can see that the code
15
00:00:40,500 --> 00:00:42,720
to which it is transformed in the end
16
00:00:42,720 --> 00:00:45,500
looks like this here, right?
17
00:00:45,500 --> 00:00:48,123
This here is the code to which it is transformed.
18
00:00:49,190 --> 00:00:53,320
Now this is a code which still is fairly cryptic,
19
00:00:53,320 --> 00:00:55,860
because this actually is the final step
20
00:00:55,860 --> 00:00:57,680
of the transformation.
21
00:00:57,680 --> 00:01:02,680
We could also write code instead of this JSX code
22
00:01:03,180 --> 00:01:07,520
which is a bit more readable to us humans than this,
23
00:01:07,520 --> 00:01:10,250
but not as readable as the JSX code.
24
00:01:10,250 --> 00:01:12,240
I still want to show it to you
25
00:01:12,240 --> 00:01:16,290
because this shows you what this JSX code
26
00:01:16,290 --> 00:01:20,010
actually represents behind-the-scenes.
27
00:01:20,010 --> 00:01:21,320
So here we go.
28
00:01:21,320 --> 00:01:22,500
Maybe
29
00:01:22,500 --> 00:01:25,270
you already wondered about one other thing.
30
00:01:25,270 --> 00:01:27,960
In package.json, we've got a bunch of dependencies,
31
00:01:27,960 --> 00:01:29,960
and most of them are just dealing with
32
00:01:29,960 --> 00:01:32,710
the behind-the-scenes transformations.
33
00:01:32,710 --> 00:01:37,710
But we've got two dependencies which are related to React.
34
00:01:38,160 --> 00:01:40,523
React and React-dom.
35
00:01:41,480 --> 00:01:45,180
Now we are using React-dom in the index.js file,
36
00:01:45,180 --> 00:01:47,910
but we are never using React anywhere.
37
00:01:47,910 --> 00:01:51,442
We're never importing React anywhere.
38
00:01:51,442 --> 00:01:56,442
And this actually just works in this kind of project setup
39
00:01:57,050 --> 00:02:00,100
and in all modern React project setups
40
00:02:00,100 --> 00:02:03,750
which are created by create-react-app.
41
00:02:03,750 --> 00:02:06,500
In a lot of other React projects,
42
00:02:06,500 --> 00:02:08,750
which you'll find out there in the wild,
43
00:02:08,750 --> 00:02:11,870
you will actually see React imports,
44
00:02:11,870 --> 00:02:14,410
even though we don't have them here.
45
00:02:14,410 --> 00:02:18,420
Because in the past, in older React projects,
46
00:02:18,420 --> 00:02:21,080
you actually needed to
47
00:02:21,080 --> 00:02:26,080
import React from React in all your React component files.
48
00:02:27,640 --> 00:02:32,640
To be precise, in all files where you used JSX.
49
00:02:33,710 --> 00:02:38,130
You needed to always import React from React in the past.
50
00:02:38,130 --> 00:02:40,730
And you'll still see a lot of React projects out there
51
00:02:40,730 --> 00:02:42,713
which have these imports therefore.
52
00:02:43,556 --> 00:02:45,670
That we can emit this import now,
53
00:02:45,670 --> 00:02:47,930
is really just some magic being done
54
00:02:47,930 --> 00:02:50,840
by modern React project setups.
55
00:02:50,840 --> 00:02:53,270
We needed this import in the past
56
00:02:53,270 --> 00:02:55,470
because this JSX code, as I mentioned,
57
00:02:55,470 --> 00:02:57,520
is just syntactic sugar.
58
00:02:57,520 --> 00:03:00,500
And under the hood, it has actually transformed
59
00:03:00,500 --> 00:03:04,610
two methods called on this React object,
60
00:03:04,610 --> 00:03:08,860
which is why we needed to import React in the past.
61
00:03:08,860 --> 00:03:11,920
Now, let me show you what this code would look like
62
00:03:11,920 --> 00:03:15,370
if we wouldn't use this JSX syntax.
63
00:03:15,370 --> 00:03:17,540
And therefore I'll comment this out
64
00:03:17,540 --> 00:03:19,363
and return it as alternative.
65
00:03:20,810 --> 00:03:23,710
Because on this imported React object,
66
00:03:23,710 --> 00:03:27,263
there is a createElement method, which we can call.
67
00:03:28,140 --> 00:03:31,973
And when you use JSX, it's near this method which is called.
68
00:03:32,856 --> 00:03:36,190
CreateElement takes three arguments.
69
00:03:36,190 --> 00:03:39,540
The first argument is the element which should be created.
70
00:03:39,540 --> 00:03:41,650
For example, a div.
71
00:03:41,650 --> 00:03:44,140
And if it's a built-in HTML element,
72
00:03:44,140 --> 00:03:46,440
you just pass in a string
73
00:03:46,440 --> 00:03:48,943
with the name of that element, like div.
74
00:03:50,260 --> 00:03:53,370
The second argument is an object
75
00:03:53,370 --> 00:03:55,210
that configures this element.
76
00:03:55,210 --> 00:03:59,610
Specifically, an object which sets all the attributes
77
00:03:59,610 --> 00:04:01,050
of this element.
78
00:04:01,050 --> 00:04:02,910
This div has no attributes,
79
00:04:02,910 --> 00:04:05,313
so here we can pass in an empty object.
80
00:04:06,460 --> 00:04:08,680
The third argument now is the content
81
00:04:08,680 --> 00:04:11,440
between the opening and closing div tags.
82
00:04:11,440 --> 00:04:13,860
And actually it's not just a third argument.
83
00:04:13,860 --> 00:04:17,310
You can now have an infinitely long list of arguments,
84
00:04:17,310 --> 00:04:19,730
which are the different content pieces
85
00:04:19,730 --> 00:04:21,903
between the opening and closing tags.
86
00:04:22,940 --> 00:04:25,150
So here we have two elements, so we would have
87
00:04:25,150 --> 00:04:27,270
two extra arguments.
88
00:04:27,270 --> 00:04:29,940
The third argument would be our H2 element.
89
00:04:29,940 --> 00:04:32,750
So the first child in that div.
90
00:04:32,750 --> 00:04:36,401
And therefore here, again, we would call React
91
00:04:36,401 --> 00:04:37,960
createElement
92
00:04:37,960 --> 00:04:39,793
and create an H2 element here.
93
00:04:40,700 --> 00:04:43,410
And that H2 element, again, has no attributes,
94
00:04:43,410 --> 00:04:46,540
so the second argument of this second createElement call
95
00:04:46,540 --> 00:04:48,510
would be an empty object.
96
00:04:48,510 --> 00:04:51,030
And the third argument of this createElement call
97
00:04:51,030 --> 00:04:53,180
would be the content in this H2 tag,
98
00:04:53,180 --> 00:04:55,470
so this string, in this case.
99
00:04:55,470 --> 00:04:58,240
So we would pass this string here,
100
00:04:58,240 --> 00:05:01,030
and escape this single quote.
101
00:05:01,030 --> 00:05:03,480
And if we now format it, it would look like this.
102
00:05:05,810 --> 00:05:10,023
Now next to this second createElement call,
103
00:05:11,480 --> 00:05:13,840
We could have a third createElement call,
104
00:05:13,840 --> 00:05:16,910
which is the neighbor element to this H2 element,
105
00:05:16,910 --> 00:05:18,693
so this expenses component.
106
00:05:19,770 --> 00:05:21,410
Again, we create an element here,
107
00:05:21,410 --> 00:05:24,390
but now we're working with a custom component
108
00:05:24,390 --> 00:05:26,290
instead of a built-in one.
109
00:05:26,290 --> 00:05:29,080
In such cases, you don't use a string here,
110
00:05:29,080 --> 00:05:32,680
but you just point at your imported function,
111
00:05:32,680 --> 00:05:34,020
which it in the end is,
112
00:05:34,020 --> 00:05:36,873
because your components are just functions, right?
113
00:05:37,880 --> 00:05:40,310
So here we set Expenses.
114
00:05:40,310 --> 00:05:42,913
We point at Expenses as an element here.
115
00:05:44,320 --> 00:05:47,620
The second argument is an object of properties
116
00:05:47,620 --> 00:05:50,510
that should be set, of attributes that should be set,
117
00:05:50,510 --> 00:05:54,430
and here we have the items prop, the items attribute,
118
00:05:54,430 --> 00:05:56,710
so we would add an items key here.
119
00:05:56,710 --> 00:05:58,900
Keep in mind, this is just a JavaScript object,
120
00:05:58,900 --> 00:06:01,950
so we have the property name, and then a colon,
121
00:06:01,950 --> 00:06:05,073
and then the value, which here would be expenses.
122
00:06:07,160 --> 00:06:09,660
And then as a third argument,
123
00:06:09,660 --> 00:06:12,380
we have to content between the opening and closing tags.
124
00:06:12,380 --> 00:06:14,280
And in this case, there is no such contents,
125
00:06:14,280 --> 00:06:15,123
so we omit it.
126
00:06:16,400 --> 00:06:17,750
And this here
127
00:06:18,650 --> 00:06:22,040
would be the alternative to this JSX code
128
00:06:22,040 --> 00:06:24,580
using this React object.
129
00:06:24,580 --> 00:06:26,570
And therefore, if I saved that,
130
00:06:26,570 --> 00:06:27,940
we go back to the screen,
131
00:06:27,940 --> 00:06:30,203
we see exactly the same as before.
132
00:06:31,500 --> 00:06:34,480
Now, you could write your entire React app like this.
133
00:06:34,480 --> 00:06:37,330
It just turns out that this is a bit harder to read
134
00:06:37,330 --> 00:06:40,693
and a bit more cumbersome than using this JSX code.
135
00:06:41,690 --> 00:06:44,850
But that's why you needed to import React from React
136
00:06:44,850 --> 00:06:47,570
in all your component files in the past,
137
00:06:47,570 --> 00:06:50,530
because this is the under-the-hood code
138
00:06:50,530 --> 00:06:54,017
which kind of gets created automatically when you use JSX.
139
00:06:55,290 --> 00:06:57,430
Now in more modern project setups,
140
00:06:57,430 --> 00:07:00,420
you can omit this React import because
141
00:07:00,420 --> 00:07:03,270
the project setup is able to make that transformation
142
00:07:03,270 --> 00:07:05,200
without the import being added.
143
00:07:05,200 --> 00:07:07,070
But if you see component functions
144
00:07:07,070 --> 00:07:09,040
where this import is there,
145
00:07:09,040 --> 00:07:10,750
it is there because, in the past,
146
00:07:10,750 --> 00:07:12,803
you always did need to add it.
147
00:07:13,860 --> 00:07:16,810
And therefore, because of that reason,
148
00:07:16,810 --> 00:07:19,560
I will now also import it and add it
149
00:07:19,560 --> 00:07:23,070
to all components where I use some JSX code.
150
00:07:23,070 --> 00:07:25,190
Like here, index.js,
151
00:07:25,190 --> 00:07:27,452
Expenses.js,
152
00:07:27,452 --> 00:07:29,767
ExpenseItem.js,
153
00:07:29,767 --> 00:07:32,120
ExpenseDate.js,
154
00:07:32,120 --> 00:07:33,710
and Card.js.
155
00:07:33,710 --> 00:07:36,340
Again, you technically don't need to do that,
156
00:07:36,340 --> 00:07:38,930
but I'm doing it to really emphasize
157
00:07:38,930 --> 00:07:42,280
that React is still being used under the hood here
158
00:07:42,280 --> 00:07:46,160
when you use JSX code, because that code
159
00:07:46,160 --> 00:07:49,210
under the hood is transformed to something like this
160
00:07:49,210 --> 00:07:51,200
before it's then transformed even more
161
00:07:51,200 --> 00:07:53,173
to what we saw in the browser earlier.
162
00:07:54,170 --> 00:07:55,770
That's important.
163
00:07:55,770 --> 00:07:57,380
And if you have a look at this code,
164
00:07:57,380 --> 00:08:02,250
it should also be clear why you need some wrapping element
165
00:08:02,250 --> 00:08:04,430
as a root JSX element.
166
00:08:04,430 --> 00:08:08,450
Why you can't have just these two side-by-side elements
167
00:08:08,450 --> 00:08:10,060
being returned.
168
00:08:10,060 --> 00:08:13,154
Because how will you write this in the React
169
00:08:13,154 --> 00:08:14,650
createElement world?
170
00:08:14,650 --> 00:08:18,630
You, in the end, would be trying to return this,
171
00:08:18,630 --> 00:08:20,020
and that doesn't work.
172
00:08:20,020 --> 00:08:22,810
You can't return more than one thing.
173
00:08:22,810 --> 00:08:25,440
You could return an array, sure,
174
00:08:25,440 --> 00:08:27,350
but this is not an array of elements.
175
00:08:27,350 --> 00:08:30,480
This is two separate elements side by side.
176
00:08:30,480 --> 00:08:33,030
That's why you always need such a wrapper,
177
00:08:33,030 --> 00:08:35,110
because in createElement world,
178
00:08:35,110 --> 00:08:36,940
you always create one element
179
00:08:36,940 --> 00:08:40,820
which then may have more child elements.
180
00:08:40,820 --> 00:08:45,360
So that's why we have this one wrapper root JSX element
181
00:08:45,360 --> 00:08:47,150
which we always need.
182
00:08:47,150 --> 00:08:48,530
This is why we need that.
183
00:08:48,530 --> 00:08:51,680
That's the under-the-hood, behind-the-scenes stuff
184
00:08:51,680 --> 00:08:52,773
that is going on.
185
00:08:53,750 --> 00:08:54,880
Now, I will comment this out
186
00:08:54,880 --> 00:08:57,100
and go back to the JSX solution,
187
00:08:57,100 --> 00:08:59,250
because, of course, we're going to write JSX code
188
00:08:59,250 --> 00:09:00,120
in the course.
189
00:09:00,120 --> 00:09:02,520
After all, this is way more convenient.
190
00:09:02,520 --> 00:09:05,080
But it is important to understand what is happening
191
00:09:05,080 --> 00:09:07,793
under the hood and how React works.
14721
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.