Skip to content

Instantly share code, notes, and snippets.

@Georgegriff
Created January 10, 2022 10:40
Show Gist options
  • Save Georgegriff/59d4eb17754e9acd5050336101b3a6d4 to your computer and use it in GitHub Desktop.
Save Georgegriff/59d4eb17754e9acd5050336101b3a6d4 to your computer and use it in GitHub Desktop.
hooks mutations - TodoProvider adding and removing items
<TodoContext.Provider
value={{
todoItems: draftTodos ? Array.from(draftTodos.values()) : [],
removeTodo: (id) => {
if (draftTodos.delete(id)) {
setTodoList(new Map(draftTodos));
}
},
addTodo: (message) => {
if (!message) {
return;
}
const todo = {
// new web api! - Support gradually increasing
id: crypto.randomUUID(),
message,
done: false,
};
if (draftTodos.has(todo.id)) return;
draftTodos.set(todo.id, todo);
setTodoList(new Map(draftTodos));
},
}}
>
{children}
</TodoContext.Provider>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment