Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,390 --> 00:00:03,940
So here's our project,
2
00:00:03,940 --> 00:00:07,060
and we already got a list of data there,
3
00:00:07,060 --> 00:00:09,020
but of course this list is static.
4
00:00:09,020 --> 00:00:11,850
It's not dynamic at all at the moment.
5
00:00:11,850 --> 00:00:14,010
So for example, if we filter here
6
00:00:14,010 --> 00:00:16,290
this does not affect the list at all,
7
00:00:16,290 --> 00:00:19,683
and also if we would add a new item here,
8
00:00:20,700 --> 00:00:24,420
you would also see that as we pick this and add this
9
00:00:24,420 --> 00:00:26,850
it's not added to the list because we haven't added
10
00:00:26,850 --> 00:00:28,840
the logic for this yet.
11
00:00:28,840 --> 00:00:30,670
So therefore that's what we're going to do,
12
00:00:30,670 --> 00:00:32,820
and therefore, here in this application
13
00:00:32,820 --> 00:00:35,520
we now wanna continue working on that.
14
00:00:35,520 --> 00:00:38,930
In the expenses JS file we currently render
15
00:00:38,930 --> 00:00:43,590
our list of expenses by individually adding
16
00:00:43,590 --> 00:00:47,860
these expense item elements here in our JSX code,
17
00:00:47,860 --> 00:00:50,930
and that's of course not dynamic at all.
18
00:00:50,930 --> 00:00:54,370
In most web applications we don't know in advance
19
00:00:54,370 --> 00:00:56,390
how many items we wanna render.
20
00:00:56,390 --> 00:00:59,600
I mean, here for example, we don't know how many expenses
21
00:00:59,600 --> 00:01:02,440
the user is going to add and how many expenses
22
00:01:02,440 --> 00:01:05,970
should be displayed when a certain year is selected.
23
00:01:05,970 --> 00:01:09,290
So therefore hard coding the number of expense items
24
00:01:09,290 --> 00:01:13,810
as we're currently doing it is definitely not the way to go.
25
00:01:13,810 --> 00:01:16,690
Instead we wanna render that list dynamically,
26
00:01:16,690 --> 00:01:18,970
and doing that as a standard case,
27
00:01:18,970 --> 00:01:21,930
which you'll face in a lot of projects and applications,
28
00:01:21,930 --> 00:01:25,770
and it's therefore also straightforward to do with React.
29
00:01:25,770 --> 00:01:27,930
So here is how it does work.
30
00:01:27,930 --> 00:01:32,750
We wanna render our expenses which are defined in App.js.
31
00:01:32,750 --> 00:01:35,550
Here we got this expenses array,
32
00:01:35,550 --> 00:01:38,920
and we wanna render this in the expenses component,
33
00:01:38,920 --> 00:01:41,670
therefore, the first step is to pass
34
00:01:41,670 --> 00:01:45,560
our expenses down via props, which we're already doing.
35
00:01:45,560 --> 00:01:47,720
We are passing the items prop,
36
00:01:47,720 --> 00:01:49,973
which points at this expenses array.
37
00:01:51,030 --> 00:01:54,970
So in the expenses component, we do get our list of expenses
38
00:01:54,970 --> 00:01:57,000
but we're currently not using that,
39
00:01:57,000 --> 00:01:58,810
and that's what we wanna change.
40
00:01:58,810 --> 00:02:01,950
On our props we got this items prop,
41
00:02:01,950 --> 00:02:04,400
and the value of that will be that array,
42
00:02:04,400 --> 00:02:07,229
which will be in the end when I render here,
43
00:02:07,229 --> 00:02:09,232
but we don't wanna output the array
44
00:02:09,232 --> 00:02:10,550
as text or anything like that.
45
00:02:10,550 --> 00:02:13,970
Instead, we wanna render one expense item
46
00:02:13,970 --> 00:02:18,970
per element in the array, and that's straightforward to do.
47
00:02:19,220 --> 00:02:22,580
For this, we first of all need single curly braces,
48
00:02:22,580 --> 00:02:25,520
opening and closing, because we wanna execute
49
00:02:25,520 --> 00:02:29,460
a dynamic expression in our JSX code,
50
00:02:29,460 --> 00:02:32,610
and you learned that that's done with curly braces.
51
00:02:32,610 --> 00:02:36,380
Then we can execute JavaScript expressions here.
52
00:02:36,380 --> 00:02:39,370
Now the expression which I do want to execute here
53
00:02:39,370 --> 00:02:42,970
is that I reach out to my array of data,
54
00:02:42,970 --> 00:02:45,820
in this case the array of expenses,
55
00:02:45,820 --> 00:02:47,580
and that then for every element
56
00:02:47,580 --> 00:02:51,220
we wanna create such a JSX element.
57
00:02:51,220 --> 00:02:53,630
For this we can access props items,
58
00:02:53,630 --> 00:02:57,730
that's our array of items, our array of expenses,
59
00:02:57,730 --> 00:03:01,160
and now we can use a built in array method,
60
00:03:01,160 --> 00:03:04,170
which is built into standard JavaScript,
61
00:03:04,170 --> 00:03:06,590
and that's the map method.
62
00:03:06,590 --> 00:03:08,530
You can learn more about this method
63
00:03:08,530 --> 00:03:10,963
if you Google for JS array map,
64
00:03:11,880 --> 00:03:15,820
and what this method does, is that it creates a new array
65
00:03:15,820 --> 00:03:19,800
based on another array, and that basically transforms
66
00:03:19,800 --> 00:03:23,150
every element in that original array.
67
00:03:23,150 --> 00:03:25,270
So here in this example, you, for example,
68
00:03:25,270 --> 00:03:27,090
see that we have an array of numbers,
69
00:03:27,090 --> 00:03:30,730
and with map we multiply every number with two
70
00:03:30,730 --> 00:03:33,783
so that we get this new array as a result,
71
00:03:35,070 --> 00:03:38,410
and for applying this transformation, map,
72
00:03:38,410 --> 00:03:40,120
this method which we can call,
73
00:03:40,120 --> 00:03:43,630
takes a function, which we pass as an argument,
74
00:03:43,630 --> 00:03:46,450
and that function is then executed
75
00:03:46,450 --> 00:03:50,960
for every item in the array on which we're calling map,
76
00:03:50,960 --> 00:03:53,920
and the result of this function is the element
77
00:03:53,920 --> 00:03:56,610
which will be added to the newly created array.
78
00:03:56,610 --> 00:03:59,640
That's how this works, and that's a built in method,
79
00:03:59,640 --> 00:04:02,130
as I said, and we can utilize it here
80
00:04:02,130 --> 00:04:06,690
to transform our array of objects, which we have here,
81
00:04:06,690 --> 00:04:08,810
we got a bunch of objects in there,
82
00:04:08,810 --> 00:04:12,690
into an array of JSX elements, to be precise,
83
00:04:12,690 --> 00:04:17,170
into an array full of expense item JSX elements,
84
00:04:17,170 --> 00:04:22,170
because if you do output an array of JSX elements here,
85
00:04:23,350 --> 00:04:28,350
like a couple of cards, then React is capable
86
00:04:28,350 --> 00:04:31,290
of simply rendering these elements.
87
00:04:31,290 --> 00:04:33,180
So if you had something like this,
88
00:04:33,180 --> 00:04:37,290
an array of JSX elements as part of your JSX code,
89
00:04:37,290 --> 00:04:41,860
react would simply render these elements side by side,
90
00:04:41,860 --> 00:04:44,460
and that's what we utilize here to transform
91
00:04:44,460 --> 00:04:48,370
our array of objects into such an array of expense items,
92
00:04:48,370 --> 00:04:50,883
which then are rendered by React.
93
00:04:52,040 --> 00:04:55,030
So map, as I just showed you, takes a function
94
00:04:55,030 --> 00:04:57,790
as a argument, and that function executes
95
00:04:57,790 --> 00:05:01,390
for every element in the array, and it gets that element
96
00:05:01,390 --> 00:05:05,100
for which it's currently executing as a parameter.
97
00:05:05,100 --> 00:05:08,620
So here we get our expense, for example,
98
00:05:08,620 --> 00:05:10,770
and then in the function body,
99
00:05:10,770 --> 00:05:12,600
so on the right side of this arrow
100
00:05:12,600 --> 00:05:14,740
if we're using our arrow function,
101
00:05:14,740 --> 00:05:18,140
we then have to return the JSX element
102
00:05:18,140 --> 00:05:21,110
into which we want to map this expense.
103
00:05:21,110 --> 00:05:25,890
So in this case, I wanna map every expense
104
00:05:25,890 --> 00:05:29,440
in my expenses array, I want to map every expense
105
00:05:29,440 --> 00:05:32,053
into an expense item JSX element.
106
00:05:33,090 --> 00:05:36,230
So I wanna transform the expense object
107
00:05:36,230 --> 00:05:40,880
to this special kind of object, to this JSX element,
108
00:05:40,880 --> 00:05:43,670
and then we can just configure this as we did it here.
109
00:05:43,670 --> 00:05:48,670
We add a title prop, but the title is now expense.title,
110
00:05:49,780 --> 00:05:53,340
so this expense, which has passed as a parameter
111
00:05:53,340 --> 00:05:55,750
into this function automatically,
112
00:05:55,750 --> 00:05:57,840
because that's how map works,
113
00:05:57,840 --> 00:06:01,670
that expense is used to extract the title,
114
00:06:01,670 --> 00:06:05,050
and we do the same for the amount with expense amount,
115
00:06:05,050 --> 00:06:07,140
and of course we do the same for the date
116
00:06:07,140 --> 00:06:11,100
by setting the date prop to expense date.
117
00:06:11,100 --> 00:06:15,063
Let me reformat this, and this is now how that looks like.
118
00:06:16,890 --> 00:06:20,260
Now we can get rid of the hard coded expense items here,
119
00:06:20,260 --> 00:06:24,663
and we're only left with this map expression here,
120
00:06:25,660 --> 00:06:30,423
where we transform our array to an array full of JSX items.
121
00:06:31,860 --> 00:06:35,160
Hence, if I now save this, and I reload,
122
00:06:35,160 --> 00:06:37,690
we get a warning, which you can ignore for now,
123
00:06:37,690 --> 00:06:42,038
but we also see that we get all these items being rendered
124
00:06:42,038 --> 00:06:47,038
just as before but now it's actually happening dynamically,
125
00:06:47,120 --> 00:06:50,930
which simply means that now it's based on the actual array,
126
00:06:50,930 --> 00:06:52,870
and that of course means that we can now
127
00:06:52,870 --> 00:06:55,640
also change that array and such changes
128
00:06:55,640 --> 00:06:58,650
will be reflected in that list,
129
00:06:58,650 --> 00:07:00,770
and that's what we're going to do next therefore.
130
00:07:00,770 --> 00:07:03,220
Of course, feel free to try it on your own first.
131
00:07:03,220 --> 00:07:05,733
We're going to do it together in the next lecture.
10817
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.