Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:03,000 --> 00:00:05,330
We're now loading all the review
2
00:00:05,330 --> 00:00:07,370
data from the Markdown file,
3
00:00:07,443 --> 00:00:09,383
using the front matter for
4
00:00:09,383 --> 00:00:11,323
properties like the title.
5
00:00:11,398 --> 00:00:13,511
What I'd like to do now is
6
00:00:13,511 --> 00:00:16,010
extract the code that loads the
7
00:00:16,010 --> 00:00:18,429
data into a separate function.
8
00:00:18,510 --> 00:00:22,005
This way the component will only be responsible
9
00:00:22,005 --> 00:00:23,791
for displaying the data.
10
00:00:23,865 --> 00:00:26,259
Let's start by writing a function
11
00:00:26,259 --> 00:00:27,783
in this file for now.
12
00:00:27,855 --> 00:00:29,734
It will need to be "async",
13
00:00:29,734 --> 00:00:32,078
and we can call it "getReview".
14
00:00:32,473 --> 00:00:34,691
So, we can literally move this
15
00:00:34,691 --> 00:00:36,687
code into the new function.
16
00:00:36,761 --> 00:00:39,073
And here we can return an object
17
00:00:39,073 --> 00:00:41,312
with all the review properties.
18
00:00:41,385 --> 00:00:44,417
These will be "title", "date", and "image",
19
00:00:44,417 --> 00:00:46,601
that we get from the front matter,
20
00:00:46,601 --> 00:00:50,746
but also the "html", rendered from the markdown.
21
00:00:50,746 --> 00:00:53,538
In fact, maybe we should rename this property,
22
00:00:53,538 --> 00:00:55,687
and call it "body" instead,
23
00:00:55,687 --> 00:00:59,009
because "html" is just the format.
24
00:00:59,009 --> 00:01:01,392
At this point, inside the component,
25
00:01:01,392 --> 00:01:05,358
we can get the "review" object by calling our new function,
26
00:01:05,358 --> 00:01:06,366
asynchronously.
27
00:01:06,433 --> 00:01:09,250
And all we need to do now is put "review"
28
00:01:09,250 --> 00:01:11,036
in front of each property.
29
00:01:11,104 --> 00:01:14,330
So the Heading will show the "review.title",
30
00:01:14,330 --> 00:01:18,060
then we have "review.date" and "review.image",
31
00:01:18,060 --> 00:01:20,068
and finally the "article" will
32
00:01:20,068 --> 00:01:21,807
display the "review.body".
33
00:01:21,874 --> 00:01:24,069
If we save, we should see that the
34
00:01:24,069 --> 00:01:26,264
page still looks exactly the same.
35
00:01:26,328 --> 00:01:29,049
Because we are just doing some refactoring.
36
00:01:29,049 --> 00:01:31,049
Now, if we want to make this
37
00:01:31,049 --> 00:01:32,763
function truly reusable,
38
00:01:32,835 --> 00:01:35,427
then we shouldn't have "stardew-valley"
39
00:01:35,427 --> 00:01:36,823
hard-coded inside it.
40
00:01:36,889 --> 00:01:39,206
We could specify which review to
41
00:01:39,206 --> 00:01:41,451
load when calling the function.
42
00:01:41,523 --> 00:01:44,764
Which means we need to accept that as an argument.
43
00:01:44,764 --> 00:01:46,640
I'll call it the "slug",
44
00:01:46,640 --> 00:01:49,956
because it's also what we use in the page URL,
45
00:01:49,956 --> 00:01:52,141
to identify each review.
46
00:01:52,141 --> 00:01:54,204
We should now use that argument
47
00:01:54,204 --> 00:01:55,667
when reading the file.
48
00:01:55,761 --> 00:01:57,798
Let's insert the "slug" here
49
00:01:57,798 --> 00:01:59,688
into the template literal.
50
00:01:59,761 --> 00:02:02,849
And if we save, everything should still work.
51
00:02:02,849 --> 00:02:06,637
So, all the logic about how to load the data
52
00:02:06,637 --> 00:02:09,166
is now nicely encapsulated into
53
00:02:09,166 --> 00:02:11,043
the getReview function.
54
00:02:11,124 --> 00:02:13,096
Let's move that function into a
55
00:02:13,096 --> 00:02:14,941
different file at this point,
56
00:02:15,004 --> 00:02:18,313
to keep it completely separate from the component.
57
00:02:18,313 --> 00:02:20,747
A common convention is to create
58
00:02:20,747 --> 00:02:23,181
a "lib" folder at the top-level,
59
00:02:23,257 --> 00:02:25,668
where to put all the code that's
60
00:02:25,668 --> 00:02:27,249
not React components.
61
00:02:27,325 --> 00:02:31,351
Let's create a file in there, called "reviews.js",
62
00:02:31,351 --> 00:02:33,550
since it will contain the logic
63
00:02:33,550 --> 00:02:34,968
to load the reviews.
64
00:02:35,039 --> 00:02:37,885
And now we can simply cut and paste the
65
00:02:37,885 --> 00:02:40,585
getReview function into the new file.
66
00:02:40,658 --> 00:02:42,640
We'll also need a few imports.
67
00:02:42,640 --> 00:02:46,086
Let's go and copy them from the page file as well,
68
00:02:46,086 --> 00:02:48,037
that is quicker than importing
69
00:02:48,037 --> 00:02:49,794
each function individually.
70
00:02:49,906 --> 00:02:53,494
Ok. We just need to "export" this function,
71
00:02:53,494 --> 00:02:55,412
and it's now ready to be used
72
00:02:55,412 --> 00:02:56,867
from the other module.
73
00:02:56,933 --> 00:02:59,059
You can see how cleaner this
74
00:02:59,059 --> 00:03:00,729
component is this way.
75
00:03:00,804 --> 00:03:03,363
We simply need to import "getReview"
76
00:03:03,363 --> 00:03:04,643
from the new file,
77
00:03:04,714 --> 00:03:05,992
and that's it.
78
00:03:06,034 --> 00:03:08,365
The job of this page is simply
79
00:03:08,365 --> 00:03:10,463
to display the review data,
80
00:03:10,540 --> 00:03:13,525
it doesn't need to know how exactly
81
00:03:13,525 --> 00:03:15,230
that data is loaded.
82
00:03:15,316 --> 00:03:17,180
Potentially, we could change
83
00:03:17,180 --> 00:03:18,645
the getReview function
84
00:03:18,711 --> 00:03:20,571
to fetch the data from an
85
00:03:20,571 --> 00:03:22,431
external API for example,
86
00:03:22,506 --> 00:03:25,380
rather than loading it from local files,
87
00:03:25,380 --> 00:03:28,643
and this component wouldn't be affected at all.
88
00:03:28,643 --> 00:03:31,815
Anyway, let's make sure everything still works,
89
00:03:31,815 --> 00:03:34,333
if we reload the page in the browser.
90
00:03:34,333 --> 00:03:37,049
It's still displaying the same content.
91
00:03:37,049 --> 00:03:40,690
But we made our code cleaner and more reusable,
92
00:03:40,690 --> 00:03:43,292
by moving all the data fetching logic
93
00:03:43,292 --> 00:03:45,472
into its own separate function.
6645
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.