Skip to content

Commit 35e702c

Browse files
committed
chore: Add a delete method on the external store
1 parent 42362b7 commit 35e702c

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed
Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import PropTypes from 'prop-types'
12
import Page from '@/common/layout/page'
2-
import useTodos from '@/domain/usesyncexternalstore/todostore'
3-
4-
function UseSyncExternalStoreComponent () {
5-
const { todos, add } = useTodos()
63

4+
function UseSyncExternalStoreComponent ({
5+
todos,
6+
addTodo,
7+
deleteTodo
8+
}) {
79
return (
810
<Page>
911
<h2>
@@ -14,15 +16,26 @@ function UseSyncExternalStoreComponent () {
1416
<div>
1517
<ul>
1618
{todos.map((item, index) => (
17-
<li key={index}>{item.text}</li>
19+
<li key={index}>
20+
<span>{item.text}</span> &nbsp;
21+
<span>
22+
<button onClick={() => deleteTodo(item.id)}>[ X ]</button>
23+
</span>
24+
</li>
1825
))}
1926
</ul>
2027
</div>
2128
}
2229

23-
<button onClick={() => add()}>Add Todo</button>
30+
<button onClick={() => addTodo()}>Add Todo</button>
2431
</Page>
2532
)
2633
}
2734

35+
UseSyncExternalStoreComponent.propTypes = {
36+
todos: PropTypes.array,
37+
addTodo: PropTypes.func,
38+
deleteTodo: PropTypes.fun
39+
}
40+
2841
export default UseSyncExternalStoreComponent

client/src/domain/usesyncexternalstore/todostore.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ export default function useTodos () {
1515

1616
return {
1717
todos,
18-
add: todoStore.addTodo
18+
addTodo: todoStore.addTodo,
19+
deleteTodo: todoStore.deleteTodo
1920
}
2021
}
2122

@@ -25,6 +26,11 @@ export const todoStore = {
2526
emitChange()
2627
},
2728

29+
deleteTodo (id) {
30+
todos = todos.filter(item => item.id !== id)
31+
emitChange()
32+
},
33+
2834
subscribe (listener) {
2935
listeners = [...listeners, listener]
3036

client/src/pages/usesyncexternalstore/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
import UseSyncExternalStoreComponent from '@/components/usesyncexternalstore'
2+
import useTodos from '@/domain/usesyncexternalstore/todostore'
23

34
function UseSyncExternalStore () {
5+
const { todos, addTodo, deleteTodo } = useTodos()
6+
47
return (
5-
<UseSyncExternalStoreComponent />
8+
<UseSyncExternalStoreComponent
9+
todos={todos}
10+
addTodo={addTodo}
11+
deleteTodo={deleteTodo}
12+
/>
613
)
714
}
815

0 commit comments

Comments
 (0)