Skip to content

Instantly share code, notes, and snippets.

@vcapretz
Created January 19, 2020 16:08
Show Gist options
  • Save vcapretz/09c2f19643575982716c5b9a2ea840cf to your computer and use it in GitHub Desktop.
Save vcapretz/09c2f19643575982716c5b9a2ea840cf 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 { id, text } = action.payload
state.push({ id, text, completed: false })
},
[toggleTodo]: (state, action) => {
const todo = state.find(todo => todo.id === action.payload)
if (todo) {
todo.completed = !todo.completed
}
}
})
const store = configureStore({ reducer: todos })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment