1
- import { memo } from "react" ;
1
+ import { memo , useCallback } from "react" ;
2
2
import { render } from "react-dom" ;
3
- import { batch , mutated , useTagged } from "react-tagged-state" ;
3
+ import { createSignal , useSelector , useSignal } from "react-tagged-state" ;
4
4
5
5
const A = [ "pretty" , "large" , "big" , "small" , "tall" , "short" , "long" , "handsome" , "plain" , "quaint" , "clean" ,
6
6
"elegant" , "easy" , "angry" , "crazy" , "helpful" , "mushy" , "odd" , "unsightly" , "adorable" , "important" , "inexpensive" ,
@@ -13,50 +13,50 @@ const random = (max) => Math.round(Math.random() * 1000) % max;
13
13
14
14
let nextId = 1 ;
15
15
16
- const buildData = ( res , count ) => {
16
+ const buildData = ( count ) => {
17
+ const res = new Array ( count ) ;
18
+
17
19
for ( let index = 0 ; index < count ; index ++ ) {
18
- res . push ( {
20
+ res [ index ] = {
19
21
id : nextId ++ ,
20
22
label : `${ A [ random ( A . length ) ] } ${ C [ random ( C . length ) ] } ${ N [ random ( N . length ) ] } `
21
- } )
23
+ } ;
22
24
}
25
+
26
+ return res ;
23
27
}
24
28
25
- const data = [ ] ;
29
+ const data = createSignal ( [ ] ) ;
26
30
27
- let selected = 0 ;
31
+ let selected = createSignal ( 0 ) ;
28
32
29
33
const Row = memo ( ( { item} ) => {
30
- useTagged ( item ) ;
34
+ const isSelected = useSelector ( ( ) => item . id === selected ( ) ) ;
35
+ const handleSelect = useCallback ( ( ) => {
36
+ selected ( item . id ) ;
37
+ } , [ item . id ] ) ;
38
+ const handleRemove = useCallback ( ( ) => {
39
+ data ( ( curr ) => curr . filter ( ( { id} ) => id !== item . id ) ) ;
40
+ } , [ item . id ] ) ;
31
41
32
42
return (
33
- < tr className = { selected === item . id ? "danger" : "" } >
43
+ < tr className = { isSelected ? "danger" : "" } >
34
44
< td className = "col-md-1" > { item . id } </ td >
35
45
< td className = "col-md-4" >
36
- < a onClick = { ( ) => batch ( ( ) => {
37
- if ( selected ) {
38
- mutated ( data . find ( ( { id} ) => id === selected ) ) ;
39
- }
40
-
41
- selected = item . id ;
42
- mutated ( item ) ;
43
- } ) } > { item . label } </ a >
46
+ < a onClick = { handleSelect } > { item . label } </ a >
44
47
</ td >
45
48
< td className = "col-md-1" >
46
- < a onClick = { ( ) => {
47
- data . splice ( data . indexOf ( item ) , 1 ) ;
48
- mutated ( data ) ;
49
- } } > < span className = "glyphicon glyphicon-remove" aria-hidden = "true" /> </ a >
49
+ < a onClick = { handleRemove } > < span className = "glyphicon glyphicon-remove" aria-hidden = "true" /> </ a >
50
50
</ td >
51
51
< td className = "col-md-6" />
52
52
</ tr >
53
53
)
54
54
} ) ;
55
55
56
56
const RowList = ( ) => {
57
- useTagged ( data ) ;
57
+ const items = useSignal ( data ) ;
58
58
59
- return data . map ( ( item ) => < Row key = { item . id } item = { item } /> )
59
+ return items . map ( ( item ) => < Row key = { item . id } item = { item } /> )
60
60
} ;
61
61
62
62
const Button = ( { id, title, cb} ) => (
@@ -73,34 +73,35 @@ const Main = () => (
73
73
< div className = "col-md-6" >
74
74
< div className = "row" >
75
75
< Button id = "run" title = "Create 1,000 rows" cb = { ( ) => {
76
- data . length = 0 ;
77
- selected = 0 ;
78
- buildData ( data , 1000 ) ;
79
- mutated ( data ) ;
76
+ data ( buildData ( 1000 ) ) ;
77
+ selected ( 0 ) ;
80
78
} } />
81
79
< Button id = "runlots" title = "Create 10,000 rows" cb = { ( ) => {
82
- data . length = 0 ;
83
- selected = 0 ;
84
- buildData ( data , 10000 ) ;
85
- mutated ( data ) ;
80
+ data ( buildData ( 10000 ) ) ;
81
+ selected ( 0 ) ;
86
82
} } />
87
83
< Button id = "add" title = "Append 1,000 rows" cb = { ( ) => {
88
- buildData ( data , 1000 ) ;
89
- mutated ( data ) ;
84
+ data ( ( curr ) => curr . concat ( buildData ( 1000 ) ) ) ;
85
+ } } />
86
+ < Button id = "update" title = "Update every 10th row" cb = { ( ) => {
87
+ data ( ( curr ) => {
88
+ const copy = curr . slice ( 0 ) ;
89
+
90
+ for ( let index = 0 ; index < copy . length ; index += 10 ) {
91
+ const item = copy [ index ] ;
92
+
93
+ copy [ index ] = { id : item . id , label : item . label + " !!!" } ;
94
+ }
95
+
96
+ return copy ;
97
+ } ) ;
90
98
} } />
91
- < Button id = "update" title = "Update every 10th row" cb = { ( ) => batch ( ( ) => {
92
- for ( let index = 0 ; index < data . length ; index += 10 ) {
93
- mutated ( data [ index ] ) . label += " !!!" ;
94
- }
95
- } ) } />
96
99
< Button id = "clear" title = "Clear" cb = { ( ) => {
97
- data . length = 0 ;
98
- selected = 0 ;
99
- mutated ( data ) ;
100
+ data ( [ ] ) ;
101
+ selected ( 0 ) ;
100
102
} } />
101
103
< Button id = "swaprows" title = "Swap Rows" cb = { ( ) => {
102
- [ data [ 1 ] , data [ 998 ] ] = [ data [ 998 ] , data [ 1 ] ] ;
103
- mutated ( data ) ;
104
+ data ( ( curr ) => [ curr [ 0 ] , curr [ 998 ] , ...curr . slice ( 2 , 998 ) , curr [ 1 ] , curr [ 999 ] ] ) ;
104
105
} } />
105
106
</ div >
106
107
</ div >
0 commit comments