Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,210 --> 00:00:07,650
Now that we know the basic workings of a react application, or at least a very rudimentary one, let's
2
00:00:07,650 --> 00:00:10,490
actually go ahead and build a component.
3
00:00:10,530 --> 00:00:14,250
So I'm going to delete app.js,
4
00:00:19,710 --> 00:00:26,230
and app.css.
5
00:00:26,290 --> 00:00:27,250
All right.
6
00:00:27,550 --> 00:00:32,430
Now if I save it, it fails to compile because it cannot find app.js.
7
00:00:32,440 --> 00:00:34,550
It's attempting to import here.
8
00:00:34,570 --> 00:00:36,340
There is no file, there's no export,
9
00:00:36,640 --> 00:00:38,230
so it's not going to happen.
10
00:00:39,190 --> 00:00:41,980
So in the source directory
11
00:00:41,980 --> 00:00:45,990
create a new file and
12
00:00:46,330 --> 00:00:49,380
let's call it application
13
00:00:49,450 --> 00:00:50,260
.js
14
00:00:54,970 --> 00:00:57,270
Now in here
15
00:00:57,280 --> 00:01:04,300
the first thing we need to do is to import react and react component class so that we can create our
16
00:01:04,300 --> 00:01:05,790
own component,
17
00:01:05,980 --> 00:01:07,750
and so let's do that.
18
00:01:07,750 --> 00:01:10,450
So import React
19
00:01:13,550 --> 00:01:22,430
Component, and the curly braces there are important, from react.
20
00:01:23,690 --> 00:01:30,170
Now drop down a few lines and we're actually going to start building out our component.
21
00:01:30,560 --> 00:01:34,310
So we're going to write class Application
22
00:01:36,350 --> 00:01:41,420
extends Component, and we're going to open that up.
23
00:01:42,080 --> 00:01:45,270
Now by itself this does not export it.
24
00:01:45,290 --> 00:01:56,150
So every time we create a class that we want to use in another file, if it's gonna be imported in another
25
00:01:56,150 --> 00:01:58,630
file, we're gonna need to export it.
26
00:01:58,640 --> 00:02:03,440
So the syntax for that is export default
27
00:02:04,340 --> 00:02:14,360
and then the class name, so Application. Then we're gonna save. Now in our index.js file I'm going
28
00:02:14,360 --> 00:02:18,260
to remove that import and replace it with our new one.
29
00:02:18,260 --> 00:02:23,780
So import the exports name, so application, from,
30
00:02:26,280 --> 00:02:29,180
and here we need to specify it's in the current directory,
31
00:02:29,190 --> 00:02:30,450
so ./
32
00:02:32,940 --> 00:02:36,210
Application, and hit semicolon.
33
00:02:36,360 --> 00:02:41,370
And by itself this will still fail because it's attempting to mount app.
34
00:02:41,940 --> 00:02:44,940
We don't have a class called app. It says the app is not defined.
35
00:02:45,090 --> 00:02:54,120
So we are going to have to render our own component. So a component in React,
36
00:02:54,210 --> 00:03:00,240
so in ES6, is going to be written the same as HTML is.
37
00:03:00,240 --> 00:03:04,060
So you're going to open it with a pointy bracket,
38
00:03:04,740 --> 00:03:09,010
and then application, and then it's self closing.
39
00:03:09,210 --> 00:03:14,070
So you're going to close it. It can be self closing, for the most part it will be. We will cover
40
00:03:14,170 --> 00:03:20,730
how to wrap components later on in the course, I'm sure.
41
00:03:20,730 --> 00:03:26,130
But for right now, this is the syntax for mounting our component.
42
00:03:26,130 --> 00:03:28,260
Now if we go back to our browser,
43
00:03:30,870 --> 00:03:32,490
we don't have render.
44
00:03:32,490 --> 00:03:38,750
So this is a necessary method within our class.
45
00:03:38,880 --> 00:03:47,220
So what we need to do now is in Application, the one essential method for a React component is called
46
00:03:47,460 --> 00:03:49,740
render.
47
00:03:49,740 --> 00:03:59,000
And what this method does is it returns HTML, or text, or variables, whatever the case may be.
48
00:03:59,010 --> 00:04:07,980
So we're going to return just for right now a string, which we're going to say hello world, and then we're
49
00:04:07,980 --> 00:04:10,260
going to end that with a semicolon.
50
00:04:14,230 --> 00:04:20,950
Now if we go back to our application we see that that's exactly what we get, just a string.
51
00:04:21,130 --> 00:04:32,350
And if I right click and go to inspect, we're gonna see that it has mounted into the div by ID root.
52
00:04:32,770 --> 00:04:36,970
We also will get into React tools shortly,
53
00:04:36,970 --> 00:04:39,410
but for right now we can see that it's mounting.
54
00:04:40,600 --> 00:04:44,490
So a few things about the render method itself.
55
00:04:45,100 --> 00:04:55,960
It's like any other method. You can declare variables and whatnot and use them, and we can return
56
00:04:59,970 --> 00:05:02,450
in brackets HTML content.
57
00:05:03,180 --> 00:05:13,890
So I could change that to h1 Hello, World, and then hit save, control + s, and then open up my browser. We
58
00:05:13,890 --> 00:05:15,370
should get an H1.
59
00:05:15,390 --> 00:05:23,130
Now you'll notice that I'm not refreshing this. And that's because when when you run npm start,
60
00:05:23,160 --> 00:05:30,480
what's happening is it loads in a hot loader, which every time a change is detected it will recompile
61
00:05:30,480 --> 00:05:33,570
and then refresh any connected clients.
62
00:05:33,660 --> 00:05:35,800
In this case, this browser.
63
00:05:36,390 --> 00:05:42,010
So this is automatically updating when I write new code and save it.
64
00:05:42,210 --> 00:05:51,750
Now we can't in this return method have a top level element, and then another top level element.
65
00:05:55,080 --> 00:05:59,520
We're gonna see an error here.
66
00:05:59,780 --> 00:06:04,240
JSX expressions, this is E6 JavaScript,
67
00:06:04,500 --> 00:06:08,130
the current implementation, must have one parent element.
68
00:06:08,130 --> 00:06:11,240
This means we can only have one top level element.
69
00:06:11,940 --> 00:06:20,900
So I'm going to wrap both of these in a div and call it a day.
70
00:06:20,930 --> 00:06:21,530
There we go.
71
00:06:21,530 --> 00:06:23,190
Problem solved.
72
00:06:23,210 --> 00:06:29,060
So now if I save, you're going to see it's going to compile, and the browser automatically refreshes,
73
00:06:29,630 --> 00:06:31,230
and we get this here.
74
00:06:31,700 --> 00:06:33,320
We get this
75
00:06:37,020 --> 00:06:46,250
So that's basically how to write a render method in our own component.
76
00:06:46,260 --> 00:06:52,620
Now we can also print out variables in here and use content like that. And how we do that is within the
77
00:06:52,620 --> 00:06:56,990
render method, not the return statement, within the render method itself
78
00:06:57,000 --> 00:07:06,360
we can declare variables. So I'm going to write let, and for those of you who took
79
00:07:06,360 --> 00:07:14,880
The Complete Front-End Web Development Course, and you learn JavaScript there, we use the keyword var to
80
00:07:14,880 --> 00:07:17,130
create variables.
81
00:07:17,130 --> 00:07:19,470
And this has scoping issues.
82
00:07:19,470 --> 00:07:22,470
If we declare it here it can be used outside of this function,
83
00:07:22,470 --> 00:07:24,500
and that's not really safe.
84
00:07:24,510 --> 00:07:32,670
So we're going to always scope our variables correctly. We're gonna either use let or const.
85
00:07:33,060 --> 00:07:35,970
So const is,
86
00:07:36,370 --> 00:07:39,180
it basically means it's a constant.
87
00:07:39,180 --> 00:07:42,180
It can't be changed after it has been set.
88
00:07:42,750 --> 00:07:48,270
So we're gonna say let name equals Nick, and then
89
00:07:51,750 --> 00:07:53,010
we're gonna save it.
90
00:07:53,040 --> 00:08:01,470
And now how we actually use this within the return statement is similar to other technologies that use
91
00:08:01,470 --> 00:08:04,410
curly braces.
92
00:08:04,410 --> 00:08:14,480
If you've used any model view controller kind of framework this should be second nature to you by now.
93
00:08:15,120 --> 00:08:20,880
But in this instance, it's just one set of curly braces that wraps it, and then we print out the variable name.
94
00:08:23,240 --> 00:08:31,920
And we can load up our browser, and we see that it prints it out correctly.
95
00:08:32,410 --> 00:08:41,890
So, I think that's it as far as this goes. We've just written a component. We've mounted it into a
96
00:08:41,980 --> 00:08:44,430
DOM element by Id root.
97
00:08:44,860 --> 00:08:50,920
And in the next video we're going to talk about more special React methods for the component class.
8985
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.