You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -16,7 +16,9 @@ Whenever we are transforming data into an array of elements and put it in our Re
16
16
17
17
Arrays require a special function `React.array` to convert an `array<Jsx.element>` to render as `Jsx.element`.
18
18
19
-
```res
19
+
<CodeTablabels={["ReScript", "JS Output"]}>
20
+
21
+
```res example
20
22
type todo = {id: string, text: string}
21
23
22
24
@react.component
@@ -31,21 +33,68 @@ let make = () => {
31
33
}
32
34
```
33
35
36
+
```js
37
+
functionPlayground(props) {
38
+
var todos = [
39
+
{
40
+
id:"todo1",
41
+
text:"Todo 1"
42
+
},
43
+
{
44
+
id:"todo2",
45
+
text:"Todo 2"
46
+
}
47
+
];
48
+
var items =todos.map(function (todo) {
49
+
returnJsxRuntime.jsx("li", {
50
+
children:todo.text
51
+
}, todo.id);
52
+
});
53
+
returnJsxRuntime.jsx("ul", {
54
+
children: items
55
+
});
56
+
}
57
+
```
58
+
59
+
</CodeTab>
60
+
34
61
## Keys
35
62
36
63
Keys help React identify which elements have been changed, added, or removed throughout each render. Keys should be given to elements inside the array to give the elements a stable identity:
The best way to pick a key is to use a string that uniquely identifies a list item among its siblings. Most often you would use IDs from your data as keys:
47
94
48
-
```res
95
+
<CodeTablabels={["ReScript", "JS Output"]}>
96
+
97
+
```res prelude
49
98
type todo = {id: string, text: string}
50
99
51
100
let todos = [
@@ -58,46 +107,78 @@ let items = Array.map(todos, todo => {
58
107
})
59
108
```
60
109
110
+
```js
111
+
var todos = [
112
+
{
113
+
id:"todo1",
114
+
text:"Todo 1"
115
+
},
116
+
{
117
+
id:"todo2",
118
+
text:"Todo 2"
119
+
}
120
+
];
121
+
122
+
var items =todos.map(function (todo) {
123
+
returnJsxRuntime.jsx("li", {
124
+
children:todo.text
125
+
}, todo.id);
126
+
});
127
+
```
128
+
129
+
</CodeTab>
130
+
61
131
If you don’t have stable IDs for rendered items, you may use the item index as a key as a last resort:
62
132
63
-
```res {1..3}
64
-
let items = Array.mapWithIndex(todos, (i, todo) => {
133
+
<CodeTablabels={["ReScript", "JS Output"]}>
134
+
135
+
```res example {1..3}
136
+
let items = Array.mapWithIndex(todos, (todo, i) => {
65
137
// Only do this if items have no stable id
66
138
<li key={Int.toString(i)}>
67
-
{todo.text}
139
+
{React.string(todo.text)}
68
140
</li>
69
-
});
141
+
})
70
142
```
71
143
144
+
```js
145
+
var items =todos.map(function (todo, i) {
146
+
returnJsxRuntime.jsx("li", {
147
+
children:todo.text
148
+
}, i.toString());
149
+
});
150
+
```
151
+
152
+
</CodeTab>
153
+
154
+
72
155
### Keys Must Only Be Unique Among Siblings
73
156
74
157
Keys used within arrays should be unique among their siblings. However they don’t need to be globally unique. We can use the same keys when we produce two different arrays:
75
158
76
-
```res {6,10,17,18,25,27}
159
+
<CodeTablabels={["ReScript", "JS Output"]}>
160
+
161
+
```res example {6,10,17,18,25,27}
77
162
type post = {id: string, title: string, content: string}
{id: "2", title: "Installation", content: "You can install reason-react from npm."}
190
+
{
191
+
id: "1",
192
+
title: "Hello World",
193
+
content: "Welcome to learning ReScript & React!",
194
+
},
195
+
{
196
+
id: "2",
197
+
title: "Installation",
198
+
content: "You can install reason-react from npm.",
199
+
},
111
200
]
112
201
113
-
let blog = <Blog posts/>
202
+
let blog = <Blog posts />
203
+
```
204
+
205
+
```js
206
+
functionPlayground$Blog(props) {
207
+
var posts =props.posts;
208
+
var sidebar =JsxRuntime.jsx("ul", {
209
+
children:posts.map(function (post) {
210
+
returnJsxRuntime.jsx("li", {
211
+
children:post.title
212
+
}, post.id);
213
+
})
214
+
});
215
+
var content =posts.map(function (post) {
216
+
returnJsxRuntime.jsxs("div", {
217
+
children: [
218
+
JsxRuntime.jsx("h3", {
219
+
children:post.title
220
+
}),
221
+
JsxRuntime.jsx("p", {
222
+
children:post.content
223
+
})
224
+
]
225
+
}, post.id);
226
+
});
227
+
returnJsxRuntime.jsxs("div", {
228
+
children: [
229
+
sidebar,
230
+
JsxRuntime.jsx("hr", {}),
231
+
content
232
+
]
233
+
});
234
+
}
235
+
236
+
var Blog = {
237
+
make: Playground$Blog
238
+
};
239
+
240
+
var posts = [
241
+
{
242
+
id:"1",
243
+
title:"Hello World",
244
+
content:"Welcome to learning ReScript & React!"
245
+
},
246
+
{
247
+
id:"2",
248
+
title:"Installation",
249
+
content:"You can install reason-react from npm."
250
+
}
251
+
];
252
+
253
+
var blog =JsxRuntime.jsx(Playground$Blog, {
254
+
posts: posts
255
+
});
114
256
```
115
257
258
+
</CodeTab>
259
+
116
260
117
261
## Rendering `list` Values
118
262
119
263
In case you ever want to render a `list` of items, you can do something like this:
120
264
121
-
```res
265
+
<CodeTablabels={["ReScript", "JS Output"]}>
266
+
267
+
```res example
122
268
type todo = {id: string, text: string}
123
269
124
270
@react.component
@@ -140,6 +286,33 @@ let make = () => {
140
286
141
287
```
142
288
289
+
```js
290
+
functionPlayground(props) {
291
+
var items =Core__List.toArray({
292
+
hd: {
293
+
id:"todo1",
294
+
text:"Todo 1"
295
+
},
296
+
tl: {
297
+
hd: {
298
+
id:"todo2",
299
+
text:"Todo 2"
300
+
},
301
+
tl:/* [] */0
302
+
}
303
+
}).map(function (todo) {
304
+
returnJsxRuntime.jsx("li", {
305
+
children:todo.text
306
+
}, todo.id);
307
+
});
308
+
returnJsxRuntime.jsx("div", {
309
+
children: items
310
+
});
311
+
}
312
+
```
313
+
314
+
</CodeTab>
315
+
143
316
We use `List.toArray` to convert our list to an array before creating our `array<React.element>`. Please note that using `list` has performance impact due to extra conversion costs.
144
317
145
318
99% of the time you'll want to use arrays (seamless interop, faster JS code), but in some cases it might make sense to use a `list` to leverage advanced pattern matching features etc.
0 commit comments