Skip to content

Instantly share code, notes, and snippets.

@vcapretz
Created January 19, 2020 16:13
Show Gist options
  • Save vcapretz/3a0e4ec41ea2c21a3b0b168c78cc8e0c to your computer and use it in GitHub Desktop.
Save vcapretz/3a0e4ec41ea2c21a3b0b168c78cc8e0c to your computer and use it in GitHub Desktop.
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