File tree 1 file changed +28
-2
lines changed
1 file changed +28
-2
lines changed Original file line number Diff line number Diff line change @@ -23,14 +23,22 @@ You can see an example [here](./playground/src/app/components/button)
23
23
Because it's a powerful inmutable state tree, it allow us to do:
24
24
25
25
``` ts
26
- @ Store < UserListState >( {
26
+ interface UserListState {
27
27
users: User [],
28
+ usersMap: Record <number , User >,
29
+ }
30
+
31
+
32
+ @Store <UserListState >({
33
+ users: [],
34
+ usersMap: {},
28
35
})
29
36
export class UserListStore {
30
37
31
38
@Action (Update )
32
39
updateUsingImmer(state : UserListState , { user }: Update ) {
33
- state .users [user .id ] = user ;
40
+ const userToUpdate = state .users .find (({ id }) => id === user .id );
41
+ Object .assign (userToUpdate , user );
34
42
}
35
43
36
44
@Action (Update )
@@ -43,6 +51,24 @@ export class UserListStore {
43
51
]
44
52
}
45
53
}
54
+
55
+ // Update an object-map
56
+
57
+ @Action (Update )
58
+ updateUsingImmer(state : UserListState , { user }: Update ) {
59
+ state .usersMap [user .id ] = user ;
60
+ }
61
+
62
+ @Action (Update )
63
+ updateWithoutImmer(state : UserListState , { user }: Update ) {
64
+ return {
65
+ ... state ,
66
+ usersMap: [
67
+ ... state .usersMap ,
68
+ [user .id ]: user ,
69
+ ]
70
+ }
71
+ }
46
72
}
47
73
```
48
74
You can’t perform that action at this time.
0 commit comments