This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import produce from "immer" | |
| const updateUsers = (state, confirmedUserId) => { | |
| //users will be user state defined above | |
| return produce(state, draft => { | |
| draft[draft.findIndex(({ id }) => id === confirmedUserId)].emailConfirmed = true | |
| }) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| onClick={() => setUsers(prev => updateUsers(prev, user.id))} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const updateUsers = produce((draft, confirmedUserId) => { | |
| draft[draft.findIndex(({ id }) => id === confirmedUserId)].emailConfirmed = true | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { useImmer } from "use-immer" | |
| const [users, setUsers] = useImmer([ | |
| { id: 1, name: "Bob", age: 24, emailConfirmed: false }, | |
| { id: 2, name: "Sarah", age: 30, emailConfirmed: true }, | |
| { id: 3, name: "Kevin", age: 20, emailConfirmed: false }, | |
| ]) | |
| //when updating a user | |
| setUsers(draft => { | |
| draft[draft.findIndex(({ id }) => id === confirmedUserId)].emailConfirmed = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { produceWithPatches } from "immer" | |
| const [nextState, patches, inversePatches] = produceWithPatches((draft, confirmedUserId) => { | |
| draft[draft.findIndex(({ id }) => id === confirmedUserId)].emailConfirmed = true | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| op: 'replace', //operations | |
| path: [2, 'emailConfirmed'], //[index of updated value, property that was updated] | |
| value: true, //updated value | |
| } |
OlderNewer