Skip to content

Instantly share code, notes, and snippets.

@shanebdavis
Created December 28, 2019 23:43
Show Gist options
  • Save shanebdavis/aab139c8b469ba957a9ed931ca6cf246 to your computer and use it in GitHub Desktop.
Save shanebdavis/aab139c8b469ba957a9ed931ca6cf246 to your computer and use it in GitHub Desktop.
modular-redux-tutorial with hooks-for-redux
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