Created
March 26, 2020 02:46
-
-
Save mattbajorek/8c28bae5b3beb45cb4a0f20b7f977e05 to your computer and use it in GitHub Desktop.
todos-redux-to-react-sweet-state todos actions
This file contains 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
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