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,010
The get Static props function
2
00:00:04,010 --> 00:00:07,450
can be added to any page file
3
00:00:07,450 --> 00:00:09,060
and only there as I mentioned,
4
00:00:09,060 --> 00:00:10,930
and you need to export it.
5
00:00:10,930 --> 00:00:12,320
And when you do so,
6
00:00:12,320 --> 00:00:16,710
then next JS will also call this get static props
7
00:00:16,710 --> 00:00:21,500
function on your behalf when it pre generate a page.
8
00:00:21,500 --> 00:00:25,230
And the disfunction also signals to next JS
9
00:00:25,230 --> 00:00:28,840
that this is a page that should be pre generated.
10
00:00:28,840 --> 00:00:31,850
Now I just said that that next JS would pre render
11
00:00:31,850 --> 00:00:34,600
any page by default and that's true,
12
00:00:34,600 --> 00:00:37,630
but we'll later learn about a way of telling next JS
13
00:00:37,630 --> 00:00:39,810
to not pre render a page
14
00:00:39,810 --> 00:00:42,140
and the offer it's important to understand that
15
00:00:42,140 --> 00:00:46,050
that get static props will not tell next JS
16
00:00:46,050 --> 00:00:47,480
to not pre render,
17
00:00:47,480 --> 00:00:51,650
but instead it of kind of a confirms to next to JS that
18
00:00:51,650 --> 00:00:55,450
this page still should be pre-generated.
19
00:00:55,450 --> 00:00:58,910
But in addition to just running through the component
20
00:00:58,910 --> 00:01:00,730
and returning the JS x code,
21
00:01:00,730 --> 00:01:03,940
next JS will then also call this,
22
00:01:03,940 --> 00:01:05,860
get static prompts function.
23
00:01:05,860 --> 00:01:09,760
If it finds one in your component file.
24
00:01:09,760 --> 00:01:12,830
So therefor here let's say we want to load this product
25
00:01:12,830 --> 00:01:17,120
data dynamically but not in the way we would do it
26
00:01:17,120 --> 00:01:19,870
in a standard react app with user fact
27
00:01:19,870 --> 00:01:21,820
what showed you before.
28
00:01:21,820 --> 00:01:24,710
And therefor attached you find another file
29
00:01:24,710 --> 00:01:28,570
the dummy backend Jason file with is just example,
30
00:01:28,570 --> 00:01:29,890
dummy file are used.
31
00:01:29,890 --> 00:01:33,010
In the react example a second ago and you should add
32
00:01:33,010 --> 00:01:37,170
that in your main project folder not in public,
33
00:01:37,170 --> 00:01:41,360
not in pages just in your folder like this.
34
00:01:41,360 --> 00:01:42,680
or actually
35
00:01:42,680 --> 00:01:46,720
maybe at a data folder oops a folder not a file
36
00:01:46,720 --> 00:01:50,480
add a data folder and move that file into there
37
00:01:50,480 --> 00:01:52,453
so that you have a data folder.
38
00:01:53,520 --> 00:01:56,780
And in that data folder you have to dummy back and Jason
39
00:01:56,780 --> 00:01:58,260
File.
40
00:01:58,260 --> 00:02:01,470
And now We want to load data from that file
41
00:02:01,470 --> 00:02:03,770
for this index JS file.
42
00:02:03,770 --> 00:02:07,300
But we don't want to load it through an HTDP request.
43
00:02:07,300 --> 00:02:10,810
That's only sent from the client site after the page
44
00:02:10,810 --> 00:02:11,910
was loaded.
45
00:02:11,910 --> 00:02:15,610
Instead we wanna prefetch the data before
46
00:02:15,610 --> 00:02:17,550
we create this component.
47
00:02:17,550 --> 00:02:22,550
And before this component page gets pre rendered by next JS.
48
00:02:22,680 --> 00:02:26,700
And that's exactly why we then add this function
49
00:02:26,700 --> 00:02:31,700
which we export the async function called get static props.
50
00:02:31,970 --> 00:02:34,963
And again it needs to be called exactly like this.
51
00:02:35,820 --> 00:02:39,220
Now in this function you then always needs to
52
00:02:39,220 --> 00:02:41,293
return an object.
53
00:02:42,150 --> 00:02:45,200
An object that has a props key because
54
00:02:45,200 --> 00:02:47,460
the function is named gets static props.
55
00:02:47,460 --> 00:02:50,520
And actually what this function does is it
56
00:02:50,520 --> 00:02:54,100
prepares the props for your component.
57
00:02:54,100 --> 00:02:57,920
So this props object It prepares that,
58
00:02:57,920 --> 00:03:01,380
because next JS if you have i get static props
59
00:03:01,380 --> 00:03:05,520
function in your file we'll first execute this function.
60
00:03:05,520 --> 00:03:09,830
And then in a second step execute the component function.
61
00:03:09,830 --> 00:03:12,670
Now in a second step because that first step
62
00:03:12,670 --> 00:03:15,840
prepares the props for this component function,
63
00:03:15,840 --> 00:03:18,560
and therefor in this get static props function.
64
00:03:18,560 --> 00:03:20,640
You can run any code you want.
65
00:03:20,640 --> 00:03:22,350
Again code that will never
66
00:03:22,350 --> 00:03:25,710
be visible on the client site that fetches data,
67
00:03:25,710 --> 00:03:30,710
and exposes data through props to this homepage component.
68
00:03:31,040 --> 00:03:34,780
So here it could set props equal to an object so
69
00:03:34,780 --> 00:03:38,150
that the props I get here are still an object,
70
00:03:38,150 --> 00:03:40,120
where i have my products.
71
00:03:40,120 --> 00:03:41,990
And that could be an array where i
72
00:03:41,990 --> 00:03:44,850
for example have one object
73
00:03:44,850 --> 00:03:49,690
in that array with an idea of P one and a title
74
00:03:49,690 --> 00:03:54,150
P of product one something like this.
75
00:03:54,150 --> 00:03:57,740
So now et study props returns an object with a props
76
00:03:57,740 --> 00:03:59,980
key that's always required.
77
00:03:59,980 --> 00:04:03,300
You always must return an object with a props key
78
00:04:03,300 --> 00:04:05,640
and then the data and the props keys up to you
79
00:04:05,640 --> 00:04:07,100
but it should be an object.
80
00:04:07,100 --> 00:04:09,910
And here it's an object with a product key
81
00:04:09,910 --> 00:04:11,280
Which holds an array.
82
00:04:11,280 --> 00:04:13,623
An array of product objects.
83
00:04:14,470 --> 00:04:18,610
And now We'll receive that here on props on this homepage
84
00:04:18,610 --> 00:04:22,560
because next JS first calls this get static props function,
85
00:04:22,560 --> 00:04:24,893
and then executes this component function.
86
00:04:25,970 --> 00:04:29,260
And it does both things in advance.
87
00:04:29,260 --> 00:04:32,900
So non of that code here runs on the client side,
88
00:04:32,900 --> 00:04:36,150
instead that all happens during built time
89
00:04:36,150 --> 00:04:39,560
or in development as part of this development server,
90
00:04:39,560 --> 00:04:40,513
which we're using.
91
00:04:41,480 --> 00:04:43,680
So here on their homepage component.
92
00:04:43,680 --> 00:04:48,620
We can then expect that we do get a product key
93
00:04:48,620 --> 00:04:50,810
On our props.
94
00:04:50,810 --> 00:04:54,310
So I can use object de-structuring to pull products
95
00:04:54,310 --> 00:04:55,860
out of props.
96
00:04:55,860 --> 00:04:58,410
And I use Products here because that's the key I
97
00:04:58,410 --> 00:05:00,050
Used to down there.
98
00:05:00,050 --> 00:05:03,530
And then we can render this list dynamically.
99
00:05:03,530 --> 00:05:06,880
By accessing products and mapping that,
100
00:05:06,880 --> 00:05:10,370
such that every product is mapped into a list item,
101
00:05:10,370 --> 00:05:13,950
Where the key is the product ID.
102
00:05:13,950 --> 00:05:16,250
So this ID we got down there
103
00:05:16,250 --> 00:05:19,410
and as a value between the opening and closing lists
104
00:05:19,410 --> 00:05:22,793
item tags i output the title like this.
105
00:05:24,040 --> 00:05:25,230
And with this In place
106
00:05:25,230 --> 00:05:29,490
if we now visit our next JS application ,
107
00:05:29,490 --> 00:05:31,623
we see product one here.
108
00:05:32,540 --> 00:05:36,200
Now of course we're not fetching that data from any file,
109
00:05:36,200 --> 00:05:38,080
i have it hard coded in there
110
00:05:38,080 --> 00:05:42,790
but we already implemented get static props with that.
111
00:05:42,790 --> 00:05:44,240
And if we now again,
112
00:05:44,240 --> 00:05:47,650
view the page source off this page we see
113
00:05:47,650 --> 00:05:51,200
that this data this product one data is part
114
00:05:51,200 --> 00:05:54,430
of the page that was sent to the client.
115
00:05:54,430 --> 00:05:58,200
So fetching that product data did not happen on the client,
116
00:05:58,200 --> 00:06:00,570
It happened on the server.
117
00:06:00,570 --> 00:06:04,210
And if we would dive into the sources tab of the dev tools
118
00:06:04,210 --> 00:06:07,040
and look through the JavaScript code files,
119
00:06:07,040 --> 00:06:11,290
you wouldn't find that code anywhere in those files.
120
00:06:11,290 --> 00:06:12,410
because it's not
121
00:06:12,410 --> 00:06:15,710
part of the code that's shipped to the client side.
122
00:06:15,710 --> 00:06:17,720
And that means that the code
123
00:06:17,720 --> 00:06:21,540
in get static props can do server side things
124
00:06:21,540 --> 00:06:24,330
because it's not executed on the client side,
125
00:06:24,330 --> 00:06:27,930
So you can use credentials which your users shouldn't see
126
00:06:27,930 --> 00:06:30,530
and you can also run code that wouldn't work
127
00:06:30,530 --> 00:06:31,710
in the browser.
128
00:06:31,710 --> 00:06:34,770
Code that accesses the file system for example,
129
00:06:34,770 --> 00:06:37,573
and that's exactly what we're going to do next.
10153
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.