-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex7.js
44 lines (37 loc) · 1.28 KB
/
ex7.js
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
// Rendering Lists
// http://localhost:3000/isolated/exercise/07.js
import React, { useState } from 'react'
const allItems = [
{ id: 'apple', value: '🍎 apple' },
{ id: 'orange', value: '🍊 orange' },
{ id: 'grape', value: '🍇 grape' },
{ id: 'pear', value: '🍐 pear' },
]
function App() {
const [items, setItems] = useState()
function addItem() {
const itemIds = items.map(i => i.id)
setItems([...items, allItems.find(i => !itemIds.includes(i.id))])
}
function removeItem(item) {
setItems(items.filter(i => i.id !== item.id))
}
return (
<div className="keys">
<button disabled={items.length >= allItems.length} onClick={addItem}>
add item
</button>
<ul>
{items.map(item => (
// 🐨 add a key prop to the <li> below. Set it to item.id
<li key={item.id}>
<button onClick={() => removeItem(item)}>remove</button>{' '}
<label htmlFor={`${item.id}-input`}>{item.value}</label>{' '}
<input id={`${item.id}-input`} defaultValue={item.value} />
</li>
))}
</ul>
</div>
)
}
export default App