File tree Expand file tree Collapse file tree 3 files changed +34
-8
lines changed
components/usesyncexternalstore
domain/usesyncexternalstore
pages/usesyncexternalstore Expand file tree Collapse file tree 3 files changed +34
-8
lines changed Original file line number Diff line number Diff line change 1+ import PropTypes from 'prop-types'
12import 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 >
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+
2841export default UseSyncExternalStoreComponent
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 11import UseSyncExternalStoreComponent from '@/components/usesyncexternalstore'
2+ import useTodos from '@/domain/usesyncexternalstore/todostore'
23
34function 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
You can’t perform that action at this time.
0 commit comments