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,290
So, now that we had a first glimpse
2
00:00:04,290 --> 00:00:08,240
at this file-based routing system in theory,
3
00:00:08,240 --> 00:00:10,210
let's apply it in practice
4
00:00:10,210 --> 00:00:12,550
to this demo project we set up here,
5
00:00:12,550 --> 00:00:15,320
so that we really understand it.
6
00:00:15,320 --> 00:00:18,400
This pages folder is important as you learned.
7
00:00:18,400 --> 00:00:21,780
And in this pages folder, we can add a new file.
8
00:00:21,780 --> 00:00:23,110
We can add a new file
9
00:00:23,110 --> 00:00:26,820
and we can, for example, name it, index.js.
10
00:00:26,820 --> 00:00:31,000
You'll learn that index.js is a special file name,
11
00:00:31,000 --> 00:00:36,000
such that Next.js will not use the file name as a path then,
12
00:00:36,870 --> 00:00:40,390
but instead will load the index.js component
13
00:00:40,390 --> 00:00:45,210
as the main component for slash nothing.
14
00:00:45,210 --> 00:00:48,240
So, here in this case, we have index.js
15
00:00:48,240 --> 00:00:52,090
and this file would be loaded if we just visit our domain
16
00:00:52,090 --> 00:00:55,840
and then we have no other path after our domain.
17
00:00:55,840 --> 00:00:58,383
So, just our domain slash nothing.
18
00:00:59,300 --> 00:01:02,863
But what goes into this index.js file?
19
00:01:03,740 --> 00:01:08,650
Well, as mentioned before, just a regular React component.
20
00:01:08,650 --> 00:01:11,040
There is nothing special to that.
21
00:01:11,040 --> 00:01:13,570
You don't need any special code in here.
22
00:01:13,570 --> 00:01:16,620
You just add our normal React component,
23
00:01:16,620 --> 00:01:18,750
you export this component
24
00:01:18,750 --> 00:01:23,170
and then, with that, Next.js will render that component
25
00:01:23,170 --> 00:01:26,430
when a request reaches this file
26
00:01:26,430 --> 00:01:29,670
through next.js routing system.
27
00:01:29,670 --> 00:01:32,510
So therefore, in this file, we can add a component
28
00:01:32,510 --> 00:01:34,850
and we can add a class-based component
29
00:01:34,850 --> 00:01:36,760
or a function-based component.
30
00:01:36,760 --> 00:01:39,960
And I will stick to function-based components only
31
00:01:39,960 --> 00:01:41,620
in this course.
32
00:01:41,620 --> 00:01:43,470
So therefore, we can add a new component,
33
00:01:43,470 --> 00:01:44,940
a new function here,
34
00:01:44,940 --> 00:01:49,200
and name this, HomePage, for example.
35
00:01:49,200 --> 00:01:51,240
Now, the name is totally up to you.
36
00:01:51,240 --> 00:01:53,790
This doesn't have to be named HomePage,
37
00:01:53,790 --> 00:01:55,780
any name is okay.
38
00:01:55,780 --> 00:01:58,300
I'm just using HomePage as a name here
39
00:01:58,300 --> 00:02:01,120
because it will be the root page loaded.
40
00:02:01,120 --> 00:02:04,350
And whilst it is a regular component, technically,
41
00:02:04,350 --> 00:02:06,610
we will treat it as a page.
42
00:02:06,610 --> 00:02:08,750
It will occupy the entire page.
43
00:02:08,750 --> 00:02:12,220
And that's why I add page in the component name.
44
00:02:12,220 --> 00:02:15,283
But you can name this component however you want.
45
00:02:16,580 --> 00:02:19,280
Now, in this component, we then wanna return
46
00:02:19,280 --> 00:02:22,290
our JSX code just as we know it.
47
00:02:22,290 --> 00:02:24,270
So, here I will return,
48
00:02:24,270 --> 00:02:26,840
let's say, a div and end that div,
49
00:02:26,840 --> 00:02:31,120
a simple H1 tag where I say, The Home Page.
50
00:02:31,120 --> 00:02:34,890
Definitely not too inspiring yet, but we'll get there.
51
00:02:34,890 --> 00:02:39,710
Now we need to export this function as the file default.
52
00:02:39,710 --> 00:02:41,600
So, either like this
53
00:02:41,600 --> 00:02:44,060
or what I like to do,
54
00:02:44,060 --> 00:02:45,510
but it's up to you,
55
00:02:45,510 --> 00:02:46,423
like this.
56
00:02:47,270 --> 00:02:50,060
We need to export this function, this component,
57
00:02:50,060 --> 00:02:52,070
so that Next.js knows
58
00:02:52,070 --> 00:02:54,700
which components should be rendered as a page
59
00:02:54,700 --> 00:02:55,840
because in theory,
60
00:02:55,840 --> 00:02:58,660
you could of course define multiple components
61
00:02:58,660 --> 00:03:01,233
and have multiple functions in that file.
62
00:03:02,450 --> 00:03:05,510
Now, with all of that, once you've saved that file,
63
00:03:05,510 --> 00:03:07,290
we can see it in action.
64
00:03:07,290 --> 00:03:09,300
For this, in your terminal here,
65
00:03:09,300 --> 00:03:11,720
simply run, npm run dev,
66
00:03:11,720 --> 00:03:13,930
to bring up the development server
67
00:03:13,930 --> 00:03:16,950
for this Next.js application.
68
00:03:16,950 --> 00:03:21,340
It will open up on localhost 3000 by default.
69
00:03:21,340 --> 00:03:24,230
And therefore, once you visit that in the browser,
70
00:03:24,230 --> 00:03:25,840
you should see this.
71
00:03:25,840 --> 00:03:27,470
And it's just a bit big here for me,
72
00:03:27,470 --> 00:03:30,513
because I zoomed in quite a lot so that you can see this.
73
00:03:31,410 --> 00:03:33,600
So, that's what you should see on the screen.
74
00:03:33,600 --> 00:03:35,840
And of course, that's the content returned
75
00:03:35,840 --> 00:03:38,980
by this component in our index.js file.
76
00:03:38,980 --> 00:03:40,510
And we do see this on the screen
77
00:03:40,510 --> 00:03:44,920
because we're visiting our domain slash nothing.
78
00:03:44,920 --> 00:03:46,760
There's nothing after that slash.
79
00:03:46,760 --> 00:03:49,610
If I would enter about here, it would crash,
80
00:03:49,610 --> 00:03:52,640
and we would get this default 404 page,
81
00:03:52,640 --> 00:03:54,330
which we get here during development,
82
00:03:54,330 --> 00:03:57,370
because we didn't define a component for that yet.
83
00:03:57,370 --> 00:03:59,770
Well, that's what we're going to do next though.
6372
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.