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 | |
| } |
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
| 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
| 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
| 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
| 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
| //confirmedUserId derived from email confirmation | |
| setUsers(prevState => prevState.map(user => (user.id === confirmedUserId ? { ...user, emailConfirmed: true } : user))) |
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 [users, setUsers] = useState([ | |
| { id: 1, name: "Bob", age: 24, emailConfirmed: false }, | |
| { id: 2, name: "Sarah", age: 30, emailConfirmed: true }, | |
| { id: 3, name: "Kevin", age: 20, emailConfirmed: false }, | |
| ]) |
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 initialFormState = { | |
| email: "", | |
| password: "", | |
| valid: false, | |
| } | |
| function Form() { | |
| const [form, setForm] = useState(initialFormState) | |
| //when updating the password field |
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
| state.hobbies.push("reading") |
NewerOlder