-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03-useState-array.jsx
42 lines (38 loc) · 928 Bytes
/
03-useState-array.jsx
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
import { data } from "../../../data";
import React from "react";
const UseStateArray = () => {
const [people, setPeople] = React.useState(data);
const removeItem = (id) => {
console.log(id);
const newPeople = people.filter((person) => person.id !== id);
setPeople(newPeople);
};
const clearAllItems = () => {
setPeople([]);
};
return (
<div>
{people.map((person) => {
const { id, name } = person;
// console.log(person);
return (
<div key={id}>
<h4>{name}</h4>
<button type="button" onClick={() => removeItem(id)}>
Delete Item
</button>
</div>
);
})}
<button
type="button"
onClick={clearAllItems}
style={{ marginTop: "2rem" }}
className="btn"
>
Clear All Items
</button>
</div>
);
};
export default UseStateArray;