Skip to content

Instantly share code, notes, and snippets.

@indreklasn
Created September 15, 2020 11:30
Show Gist options
  • Save indreklasn/f503de674ba45c1c91a9a12b9b28c68a to your computer and use it in GitHub Desktop.
Save indreklasn/f503de674ba45c1c91a9a12b9b28c68a to your computer and use it in GitHub Desktop.
import { createReducer } from '@reduxjs/toolkit'
const todosReducer = createReducer([], {
ADD_TODO: (state, action) => {
// "mutate" the array by calling push()
state.push(action.payload)
},
TOGGLE_TODO: (state, action) => {
const todo = state[action.payload.index]
// "mutate" the object by overwriting a field
todo.completed = !todo.completed
},
REMOVE_TODO: (state, action) => {
// Can still return an immutably-updated value if we want to
return state.filter((todo, i) => i !== action.payload.index)
}
})
export default todosReducer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment