Skip to content

Instantly share code, notes, and snippets.

@shanebdavis
Created November 21, 2019 22:33
Show Gist options
  • Save shanebdavis/33dd7b1b37373d2f3dc0b708ae95bfcc to your computer and use it in GitHub Desktop.
Save shanebdavis/33dd7b1b37373d2f3dc0b708ae95bfcc to your computer and use it in GitHub Desktop.
The heart of the Todo app's hooks-for-redux implementation
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