Skip to content

Commit

Permalink
misc(README): update immer example
Browse files Browse the repository at this point in the history
  • Loading branch information
tonivj5 committed Mar 10, 2019
1 parent 57428a8 commit ec6a077
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,22 @@ You can see an example [here](./playground/src/app/components/button)
Because it's a powerful inmutable state tree, it allow us to do:

```ts
@Store<UserListState>({
interface UserListState {
users: User[],
usersMap: Record<number, User>,
}


@Store<UserListState>({
users: [],
usersMap: {},
})
export class UserListStore {

@Action(Update)
updateUsingImmer(state: UserListState, { user }: Update) {
state.users[user.id] = user;
const userToUpdate = state.users.find(({ id }) => id === user.id);
Object.assign(userToUpdate, user);
}

@Action(Update)
Expand All @@ -43,6 +51,24 @@ export class UserListStore {
]
}
}

// Update an object-map

@Action(Update)
updateUsingImmer(state: UserListState, { user }: Update) {
state.usersMap[user.id] = user;
}

@Action(Update)
updateWithoutImmer(state: UserListState, { user }: Update) {
return {
...state,
usersMap: [
...state.usersMap,
[user.id]: user,
]
}
}
}
```

Expand Down

0 comments on commit ec6a077

Please sign in to comment.