Skip to content

Commit ec6a077

Browse files
committed
misc(README): update immer example
1 parent 57428a8 commit ec6a077

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

README.md

+28-2
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,22 @@ You can see an example [here](./playground/src/app/components/button)
2323
Because it's a powerful inmutable state tree, it allow us to do:
2424

2525
```ts
26-
@Store<UserListState>({
26+
interface UserListState {
2727
users: User[],
28+
usersMap: Record<number, User>,
29+
}
30+
31+
32+
@Store<UserListState>({
33+
users: [],
34+
usersMap: {},
2835
})
2936
export class UserListStore {
3037

3138
@Action(Update)
3239
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);
3442
}
3543

3644
@Action(Update)
@@ -43,6 +51,24 @@ export class UserListStore {
4351
]
4452
}
4553
}
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+
}
4672
}
4773
```
4874

0 commit comments

Comments
 (0)