Skip to content

Commit b870865

Browse files
committed
chore: Display todo list state data from an inner component
1 parent 37fe6eb commit b870865

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

client/src/components/usestate/index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { useState } from 'react'
22
import Card from '@/common/ui/card'
33
import Page from '@/common/layout/page'
4+
45
import TodoListComponentV3 from '@/domain/usestate/todolist'
6+
import TodoListComponentFull from '@/domain/usestate/todolistfull'
57

68
function 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
}

client/src/domain/usestate/todolist/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ function TodoListComponentV3 ({
2121
}
2222

2323
TodoListComponentV3.propTypes = {
24+
todos: PropTypes.array,
2425
deleteTodo: PropTypes.func
2526
}
2627

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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

0 commit comments

Comments
 (0)