Created
November 21, 2019 22:33
-
-
Save shanebdavis/33dd7b1b37373d2f3dc0b708ae95bfcc to your computer and use it in GitHub Desktop.
The heart of the Todo app's hooks-for-redux implementation
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
import { useRedux } from "hooks-for-redux"; | |
const getUniqueId = list => | |
list.length > 0 ? Math.max(...list.map(t => t.id)) + 1 : 1; | |
export const [useList, { addItem, deleteItem }] = useRedux( | |
"list", | |
[ | |
{ id: 1, text: "clean the house" }, | |
{ id: 2, text: "buy milk" } | |
], | |
{ | |
addItem: (list, item) => [...list, { ...item, id: getUniqueId(list) }], | |
deleteItem: (list, item) => list.filter(todo => todo.id !== item.id) | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment