-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mutating state #32
Comments
I also did not like the way My solution was to use let completed = undefined
const notes = state.notes.map((note) => {
if (note.id === id) {
completed = !note.completed
return { ...note, completed }
}
return note
}) Thus, the complete const updateNote = async ({ id }) => {
let completed = undefined
const notes = state.notes.map((note) => {
if (note.id === id) {
completed = !note.completed
return { ...note, completed }
}
return note
})
try {
await API.graphql({
query: UPDATE_NOTE,
variables: {
input: { id, completed },
},
})
console.log('successfully updated note')
} catch (error) {
console.log('error: ', error)
}
dispatch({ type: 'SET_NOTES', notes })
} The entire example can be found here, if some one is interested. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think you are mutating the state at the following lines:
full-stack-serverless-code/graphql/src/App.js
Lines 83 to 84 in 44c4eb2
Since each
note
is an object, settingnotes[index].completed
property is mutating thenote
object. Instead, we should create a newnote
object like this:The text was updated successfully, but these errors were encountered: