-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.7.tsx
184 lines (152 loc) · 5.6 KB
/
1.7.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/***************** Rendering Lists *********************/
/**
* - You will often want to display multiple similar components from a collection of data.
* You can use the JavaScript array methods to manipulate an array of data. On this page,
* you’ll use filter() and map() with React to filter and transform your array of data into
* an array of components.
*/
/******* Rendering data from arrays ********/
/**
* Here’s a short example of how to generate a list of items from an array:
*
* 1. Move the data into an array:
const people = [
'Creola Katherine Johnson: mathematician',
'Mario José Molina-Pasquel Henríquez: chemist',
'Mohammad Abdus Salam: physicist',
'Percy Lavon Julian: chemist',
'Subrahmanyan Chandrasekhar: astrophysicist'
];
* 2. Map the people members into a new array of JSX nodes, listItems:
const listItems = people.map(person => <li>{person}</li>);
* 3. Return listItems from your component wrapped in a <ul>:
return <ul>{listItems}</ul>;
*/
/******* Filtering arrays of items ********/
/**
* This data can be structured even more.
*
const people = [{
id: 0,
name: 'Creola Katherine Johnson',
profession: 'mathematician',
}, {
id: 1,
name: 'Mario José Molina-Pasquel Henríquez',
profession: 'chemist',
}, {
id: 2,
name: 'Mohammad Abdus Salam',
profession: 'physicist',
}, {
name: 'Percy Lavon Julian',
profession: 'chemist',
}, {
name: 'Subrahmanyan Chandrasekhar',
profession: 'astrophysicist',
}];
* - Let’s say you want a way to only show people whose profession is 'chemist'. You can
* use JavaScript’s filter() method to return just those people:
const chemists = people.filter(person =>
person.profession === 'chemist'
);
const listItems = chemists.map(person =>
<li>
<img
src={getImageUrl(person)}
alt={person.name}
/>
<p>
<b>{person.name}:</b>
{' ' + person.profession + ' '}
known for {person.accomplishment}
</p>
</li>
);
return <ul>{listItems}</ul>;
*/
/**
* - Arrow functions implicitly return the expression right after =>, so you didn’t need a
* return statement. However, you must write return explicitly if your => is followed
* by a { curly brace!
*/
/**
* Keeping list items in order with key
*
* - You need to give each array item a key — a string or a number that uniquely identifies
* it among other items in that array:
<li key={person.id}>...</li>
* - JSX elements directly inside a map() call always need keys!
* - Rather than generating keys on the fly, you should include them in your data:
export const people = [{
id: 0, // Used in JSX as a key
name: 'Creola Katherine Johnson',
profession: 'mathematician',
accomplishment: 'spaceflight calculations',
imageId: 'MK3eW3A'
}, {
id: 1, // Used in JSX as a key
name: 'Mario José Molina-Pasquel Henríquez',
profession: 'chemist',
accomplishment: 'discovery of Arctic ozone hole',
imageId: 'mynHUSa'
}, {
id: 2, // Used in JSX as a key
name: 'Mohammad Abdus Salam',
profession: 'physicist',
accomplishment: 'electromagnetism theory',
imageId: 'bE7W1ji'
}];
*
*/
/**
* Displaying several DOM nodes for each list item
*
* - What do you do when each item needs to render not one, but several DOM nodes?
* The short <>...</> Fragment syntax won’t let you pass a key, so you need to either group
* them into a single <div>, or use the slightly longer and more explicit <Fragment> syntax:
const listItems = people.map(person =>
<Fragment key={person.id}>
<h1>{person.name}</h1>
<p>{person.bio}</p>
</Fragment>
);
*
*/
/**
*
* Where to get your key
*
* - Data from a database: If your data is coming from a database, you can use
* the database keys/IDs, which are unique by nature.
*
* - Locally generated data: If your data is generated and persisted locally (e.g. notes
* in a note-taking app), use an incrementing counter, crypto.randomUUID() or a
* package like uuid when creating items.
*
*/
/**
*
* Rules of keys
*
* - Keys must be unique among siblings. However, it’s okay to use the same keys for
* JSX nodes in different arrays.
*
* - Keys must not change or that defeats their purpose! Don’t generate them while rendering.
*
*/
/**
* Pitfall
*
* - Never use the index of an array as a key, that's what React does by default and it might cause a bug
*
* - Do not generate keys on the fly like: Math.random(). This will cause keys to never match up
* between renders, leading to all your components and DOM being recreated every time. Not only
* is this slow, but it will also lose any user input inside the list items. Instead, use a stable
* ID based on the data.
*
* - Note that your components won’t receive key as a prop. It’s only used as a hint by React
* itself. If your component needs an ID, you have to pass it as a separate prop
*
*
*/