Skip to content

Instantly share code, notes, and snippets.

@mattbajorek
Created March 26, 2020 02:46
Show Gist options
  • Save mattbajorek/8c28bae5b3beb45cb4a0f20b7f977e05 to your computer and use it in GitHub Desktop.
Save mattbajorek/8c28bae5b3beb45cb4a0f20b7f977e05 to your computer and use it in GitHub Desktop.
todos-redux-to-react-sweet-state todos actions
let nextTodoId = 0
const todosActions = {
addTodo: text => ({ setState }) => {
setState(todos => {
todos.push({
id: nextTodoId++,
text,
completed: false
})
})
},
toggleTodo: todoId => ({ setState }) => {
setState(todos => {
const todo = todos.find(({ id }) => id === todoId)
if (todo) {
todo.completed = !todo.completed
}
})
}
}
export default todosActions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment