Skip to content

Commit 67302c6

Browse files
committed
add click for deleting items
1 parent 3d4738d commit 67302c6

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

react-es6-spread-operator-practice/src/components/App.jsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ function App() {
1919
setItem("");
2020
}
2121

22+
function deleteItem(id) {
23+
addItems((prevValue) => {
24+
return prevValue.filter((item, idx) => {
25+
return idx !== id;
26+
});
27+
});
28+
}
29+
2230
return (
2331
<div className="container">
2432
<div className="heading">
@@ -32,8 +40,8 @@ function App() {
3240
</div>
3341
<div>
3442
<ul>
35-
{items.map((i) => (
36-
<Items text={i} />
43+
{items.map((item, idx) => (
44+
<Items key={idx} id={idx} text={item} checked={deleteItem} />
3745
))}
3846
</ul>
3947
</div>

react-es6-spread-operator-practice/src/components/Items.jsx

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
1-
import React, { useState } from "react";
1+
import React from "react";
22

33
function Items(props) {
4-
const [isClick, setClick] = useState(false);
5-
6-
function handleClick() {
7-
setClick((prevValue) => {
8-
return !prevValue;
9-
});
10-
}
11-
4+
// call `props.checked` function when click
125
return (
13-
<div onClick={handleClick}>
14-
<li style={{ textDecoration: isClick ? "line-through" : "none" }}>
15-
{props.text}
16-
</li>
6+
<div
7+
onClick={() => {
8+
props.checked(props.id);
9+
}}
10+
>
11+
<li>{props.text}</li>
1712
</div>
1813
);
1914
}

0 commit comments

Comments
 (0)