File tree Expand file tree Collapse file tree 2 files changed +18
-15
lines changed
react-es6-spread-operator-practice/src/components Expand file tree Collapse file tree 2 files changed +18
-15
lines changed Original file line number Diff line number Diff line change @@ -19,6 +19,14 @@ function App() {
19
19
setItem ( "" ) ;
20
20
}
21
21
22
+ function deleteItem ( id ) {
23
+ addItems ( ( prevValue ) => {
24
+ return prevValue . filter ( ( item , idx ) => {
25
+ return idx !== id ;
26
+ } ) ;
27
+ } ) ;
28
+ }
29
+
22
30
return (
23
31
< div className = "container" >
24
32
< div className = "heading" >
@@ -32,8 +40,8 @@ function App() {
32
40
</ div >
33
41
< div >
34
42
< ul >
35
- { items . map ( ( i ) => (
36
- < Items text = { i } />
43
+ { items . map ( ( item , idx ) => (
44
+ < Items key = { idx } id = { idx } text = { item } checked = { deleteItem } />
37
45
) ) }
38
46
</ ul >
39
47
</ div >
Original file line number Diff line number Diff line change 1
- import React , { useState } from "react" ;
1
+ import React from "react" ;
2
2
3
3
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
12
5
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 >
17
12
</ div >
18
13
) ;
19
14
}
You can’t perform that action at this time.
0 commit comments