Created
January 19, 2020 16:13
-
-
Save vcapretz/3a0e4ec41ea2c21a3b0b168c78cc8e0c 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 { createAction, createReducer, configureStore } from '@reduxjs/toolkit' | |
const addTodo = createAction('ADD_TODO') | |
const toggleTodo = createAction('TOGGLE_TODO') | |
const todos = createReducer([], { | |
[addTodo]: (state, action) => { | |
const todo = action.payload | |
return [...state, todo] | |
}, | |
[toggleTodo]: (state, action) => { | |
const index = action.payload | |
const todo = state[index] | |
return [ | |
...state.slice(0, index), | |
{ ...todo, completed: !todo.completed } | |
...state.slice(index + 1) | |
] | |
} | |
}) | |
const store = configureStore({ reducer: todos }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment