Skip to content

Commit 37fe6eb

Browse files
committed
chore: Testing page re-renders when loading local state data set by useState from deeply-nested components
1 parent 5f78c5c commit 37fe6eb

File tree

5 files changed

+104
-3
lines changed

5 files changed

+104
-3
lines changed

client/src/components/home/items.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@
66
{
77
"name": "redux toolkit",
88
"link": "/redux"
9+
},
10+
{
11+
"name": "useState",
12+
"link": "/usestate"
913
}
1014
]
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { useState } from 'react'
2+
import Card from '@/common/ui/card'
3+
import Page from '@/common/layout/page'
4+
import TodoListComponentV3 from '@/domain/usestate/todolist'
5+
6+
function UseStateComponent () {
7+
const [state, setState] = useState([])
8+
9+
const addTodo = () => {
10+
const data = [...state]
11+
data.push({
12+
id: Math.random().toString(36).substring(2, 8),
13+
text: 'Hello, wooorld!!'
14+
})
15+
16+
setState(data)
17+
}
18+
19+
const deleteTodo = (id) => {
20+
const temp = state.filter(item => item.id !== id)
21+
setState(temp)
22+
}
23+
24+
return (
25+
<Page>
26+
<h2>
27+
useState
28+
</h2>
29+
30+
<p> Testing page re-renders and local state set by useState rendering from inside a deeply-nested component.</p>
31+
<br />
32+
33+
<button onClick={addTodo}>
34+
Create Todo
35+
</button>
36+
<br /><br />
37+
38+
<Card>
39+
<Card>
40+
<Card>
41+
<Card>
42+
<Card>
43+
<Card>
44+
<TodoListComponentV3
45+
todos={state}
46+
addTodo={addTodo}
47+
deleteTodo={deleteTodo}
48+
/>
49+
</Card>
50+
</Card>
51+
</Card>
52+
</Card>
53+
</Card>
54+
</Card>
55+
</Page>
56+
)
57+
}
58+
59+
export default UseStateComponent
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import PropTypes from 'prop-types'
2+
3+
function TodoListComponentV3 ({
4+
todos,
5+
deleteTodo
6+
}) {
7+
return (
8+
<ul style={{ marginTop: '24px' }}>
9+
{(todos).map(((item, index) => (
10+
<li key={index}>
11+
<span>id: {item.id}, {item.text}</span>
12+
<span>
13+
<button onClick={() => deleteTodo(item.id)}>
14+
[ x ]
15+
</button>
16+
</span>
17+
</li>
18+
)))}
19+
</ul>
20+
)
21+
}
22+
23+
TodoListComponentV3.propTypes = {
24+
deleteTodo: PropTypes.func
25+
}
26+
27+
export default TodoListComponentV3

client/src/pages/_app.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { store } from '@/store/store'
44
import '@/styles/globals.css'
55

66
export default function App({ Component, pageProps }) {
7-
return <Provider store={store}>
8-
<Component {...pageProps} />
9-
</Provider>
7+
return (
8+
<Provider store={store}>
9+
<Component {...pageProps} />
10+
</Provider>
11+
)
1012
}

client/src/pages/usestate/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import UseStateComponent from '@/components/usestate'
2+
3+
function UseStateContainer () {
4+
return (
5+
<UseStateComponent />
6+
)
7+
}
8+
9+
export default UseStateContainer

0 commit comments

Comments
 (0)