File tree Expand file tree Collapse file tree 3 files changed +71
-0
lines changed Expand file tree Collapse file tree 3 files changed +71
-0
lines changed Original file line number Diff line number Diff line change 11import { useState } from 'react'
22import Card from '@/common/ui/card'
33import Page from '@/common/layout/page'
4+
45import TodoListComponentV3 from '@/domain/usestate/todolist'
6+ import TodoListComponentFull from '@/domain/usestate/todolistfull'
57
68function UseStateComponent ( ) {
79 const [ state , setState ] = useState ( [ ] )
@@ -35,6 +37,10 @@ function UseStateComponent () {
3537 </ button >
3638 < br /> < br />
3739
40+ < h3 > ToDo list state passed from props</ h3 >
41+ < br />
42+
43+ { /** Renders a list of ToDo items passed from props */ }
3844 < Card >
3945 < Card >
4046 < Card >
@@ -52,6 +58,26 @@ function UseStateComponent () {
5258 </ Card >
5359 </ Card >
5460 </ Card >
61+
62+ < br /> < br />
63+
64+ < h3 > ToDo list state isolated on an inner component</ h3 >
65+ < br />
66+
67+ { /** Renders a list of ToDo items with all local state inside the TodoListComponentFull component */ }
68+ < Card >
69+ < Card >
70+ < Card >
71+ < Card >
72+ < Card >
73+ < Card >
74+ < TodoListComponentFull />
75+ </ Card >
76+ </ Card >
77+ </ Card >
78+ </ Card >
79+ </ Card >
80+ </ Card >
5581 </ Page >
5682 )
5783}
Original file line number Diff line number Diff line change @@ -21,6 +21,7 @@ function TodoListComponentV3 ({
2121}
2222
2323TodoListComponentV3 . propTypes = {
24+ todos : PropTypes . array ,
2425 deleteTodo : PropTypes . func
2526}
2627
Original file line number Diff line number Diff line change 1+ import { useState } from 'react'
2+
3+ function TodoListComponentFull ( ) {
4+ const [ todos , setState ] = useState ( [ ] )
5+
6+ const addTodo = ( ) => {
7+ const data = [ ...todos ]
8+ data . push ( {
9+ id : Math . random ( ) . toString ( 36 ) . substring ( 2 , 8 ) ,
10+ text : 'Hi, wooorld!!'
11+ } )
12+
13+ setState ( data )
14+ }
15+
16+ const deleteTodo = ( id ) => {
17+ const temp = todos . filter ( item => item . id !== id )
18+ setState ( temp )
19+ }
20+
21+ return (
22+ < >
23+ < button onClick = { addTodo } >
24+ Create Todo
25+ </ button >
26+ < br /> < br />
27+
28+ < ul style = { { marginTop : '24px' } } >
29+ { ( todos ) . map ( ( ( item , index ) => (
30+ < li key = { index } >
31+ < span > id: { item . id } , { item . text } </ span >
32+ < span >
33+ < button onClick = { ( ) => deleteTodo ( item . id ) } >
34+ [ x ]
35+ </ button >
36+ </ span >
37+ </ li >
38+ ) ) ) }
39+ </ ul >
40+ </ >
41+ )
42+ }
43+
44+ export default TodoListComponentFull
You can’t perform that action at this time.
0 commit comments