Created
September 15, 2020 11:30
-
-
Save indreklasn/f503de674ba45c1c91a9a12b9b28c68a to your computer and use it in GitHub Desktop.
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 { 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