Skip to content

Commit 7a4710f

Browse files
committed
chore: Minimize full page rerendering from usesyncexternalstore
1 parent 679fe7e commit 7a4710f

File tree

4 files changed

+46
-21
lines changed

4 files changed

+46
-21
lines changed
Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import PropTypes from 'prop-types'
22
import Page from '@/common/layout/page'
3+
import Card from '@/common/ui/card'
4+
import TodoListComponentV2 from '@/domain/usesyncexternalstore/todolist'
35

46
function UseSyncExternalStoreComponent ({
5-
todos,
67
addTodo,
78
deleteTodo
89
}) {
@@ -12,30 +13,26 @@ function UseSyncExternalStoreComponent ({
1213
UseSyncExternalStoreComponent
1314
</h2>
1415

15-
{(todos !== undefined) &&
16-
<div>
17-
<ul>
18-
{todos.map((item, index) => (
19-
<li key={index}>
20-
<span>{item.text}</span> &nbsp;
21-
<span>
22-
<button onClick={() => deleteTodo(item.id)}>[ X ]</button>
23-
</span>
24-
</li>
25-
))}
26-
</ul>
27-
</div>
28-
}
16+
<TodoListComponentV2
17+
deleteTodo={deleteTodo}
18+
/>
2919

30-
<button onClick={() => addTodo()}>Add Todo</button>
20+
<button onClick={() => addTodo()}>
21+
Add Todo
22+
</button>
23+
24+
<br /><br />
25+
26+
<Card>
27+
Hello
28+
</Card>
3129
</Page>
3230
)
3331
}
3432

3533
UseSyncExternalStoreComponent.propTypes = {
36-
todos: PropTypes.array,
3734
addTodo: PropTypes.func,
38-
deleteTodo: PropTypes.fun
35+
deleteTodo: PropTypes.func
3936
}
4037

4138
export default UseSyncExternalStoreComponent
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import PropTypes from 'prop-types'
2+
import useTodos from '@/lib/hooks/usetodo'
3+
4+
function TodoListComponentV2 ({
5+
deleteTodo
6+
}) {
7+
const { todos } = useTodos()
8+
9+
return (
10+
<ul style={{ marginTop: '24px' }}>
11+
{(todos).map(((item, index) => (
12+
<li key={index}>
13+
<span>id: {item.id}, {item.text}</span>
14+
<span>
15+
<button onClick={() => deleteTodo(item.id)}>
16+
[ x ]
17+
</button>
18+
</span>
19+
</li>
20+
)))}
21+
</ul>
22+
)
23+
}
24+
25+
TodoListComponentV2.propTypes = {
26+
deleteTodo: PropTypes.func
27+
}
28+
29+
export default TodoListComponentV2

client/src/pages/usesyncexternalstore/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import UseSyncExternalStoreComponent from '@/components/usesyncexternalstore'
2-
import useTodos from '@/domain/usesyncexternalstore/todostore'
2+
import useTodos from '@/lib/hooks/usetodo'
33

44
function UseSyncExternalStore () {
5-
const { todos, addTodo, deleteTodo } = useTodos()
5+
const { addTodo, deleteTodo } = useTodos()
66

77
return (
88
<UseSyncExternalStoreComponent
9-
todos={todos}
109
addTodo={addTodo}
1110
deleteTodo={deleteTodo}
1211
/>

0 commit comments

Comments
 (0)