Skip to main content

Without Service

This is an alternate version withut teh use of a service. The service is replaced by a parent component that holds the mutations and passes it down to the child components.

See it in action

Todo List

Handle by the components only

import React from 'react';
import { TodoList } from './TodoListWithProps';
import { rippleHooks } from "./Lake";

// Extract the hook from the rippleHooks
const {useTasks} = rippleHooks;

export default function() {
const [tasks, setTask] = useTasks();

return <TodoList
addTask={(text: string) => {
tasks.todo.push({ id: Math.random().toString(36).substring(2, 9), text });
setTask();
}}

finishTask={(id: string) => {
const index = tasks.todo.findIndex((task) => task.id === id);
if (index !== -1) {
const task = tasks.todo.splice(index, 1)[0];
tasks.todo = tasks.todo.filter((task) => task.id !== id);
tasks.done.push(task);
}
setTask();
}}

removeTask={(id: string) => {
const index = tasks.done.findIndex((task) => task.id === id);
if (index !== -1) {
tasks.done = tasks.done.filter((task) => task.id !== id);
}
setTask();
}}

reset={() => {
setTask("reset");
}} />;
}