From ec6a077de4540bcfb837a7048c6409b3218e79dd Mon Sep 17 00:00:00 2001 From: xxxtonixxx Date: Sun, 10 Mar 2019 13:04:54 +0100 Subject: [PATCH] misc(README): update immer example --- README.md | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 744f29e..5f11f1e 100644 --- a/README.md +++ b/README.md @@ -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({ +interface UserListState { users: User[], + usersMap: Record, +} + + +@Store({ + 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) @@ -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, + ] + } + } } ```