diff --git a/packages/kitchen-sink/src/examples/todolist.tsx b/packages/kitchen-sink/src/examples/todolist.tsx index f9573a3fa7..b9ba1566db 100644 --- a/packages/kitchen-sink/src/examples/todolist.tsx +++ b/packages/kitchen-sink/src/examples/todolist.tsx @@ -1,32 +1,25 @@ import { useState } from 'react'; -import { block } from 'million/react'; +import { block, For } from 'million/react'; -const TodoList = block(() => { - const [tasks, setTasks] = useState([]); +const TodoList = () => { + const [tasks, setTasks] = useState([]); const [taskInput, setTaskInput] = useState(''); + const [taskIdCounter, setTaskIdCounter] = useState(0); const addTask = () => { if (taskInput.trim() === '') return; - setTasks([...tasks, { text: taskInput, completed: false }]); + setTasks([ + ...tasks, + { id: taskIdCounter, text: taskInput, completed: false }, + ]); setTaskInput(''); - }; - - const toggleTaskCompletion = (index: number) => { - const updatedTasks = [...tasks]; - updatedTasks[index].completed = !updatedTasks[index].completed; - setTasks(updatedTasks); - }; - - const removeTask = (index: number) => { - const updatedTasks = [...tasks]; - updatedTasks.splice(index, 1); - setTasks(updatedTasks); + setTaskIdCounter(taskIdCounter + 1); }; return (

Todo List

-
+
event.preventDefault()}> { /> -
-
    - {tasks.map((task: any, index: number) => ( -
  • - toggleTaskCompletion(index)} - /> + + +
+ ); +}; + +//* for iterating over the list & block() for optimizing +const List = block(({ tasks, setTasks }: { tasks: any[]; setTasks: any }) => { + const toggleTaskCompletion = (index: number) => { + const updatedTasks = [...tasks]; + updatedTasks[index].completed = !updatedTasks[index].completed; + setTasks(updatedTasks); + }; + + const removeTask = (index: number) => { + const updatedTasks = tasks.filter((task) => task.id !== index); + setTasks(updatedTasks); + console.log(updatedTasks); + }; + + return ( +
    + + {(task) => ( +
  • {task.text} + toggleTaskCompletion(task.id)} + />
  • - ))} -
- + )} +
+ ); });