diff --git a/src/App.tsx b/src/App.tsx index 6d525b9..b07e132 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,4 +1,4 @@ -import { useState } from "react" +import { useMemo, useState } from "react" import { TaskItem } from "./components/TaskItem"; import { useTasks } from "./hooks/useTasks"; @@ -7,9 +7,14 @@ function App() { const [inputValue, setInputValue] = useState(''); const { tasks, addTask, deleteTask, toggleTask } = useTasks(); + const completedCount = useMemo(() => { + console.log("Counting tasks..."); + return tasks.filter(t => t.completed).length; + }, [tasks]); + return (
-

SmartX Task Manager

+

SmartX Task Manager {completedCount}

void; } -export const TaskItem = ({ task, onDelete, onToggle }: TaskItemProps) => { +export const TaskItem = memo(({ task, onDelete, onToggle }: TaskItemProps) => { return (
  • {
  • ); -}; +}); diff --git a/src/hooks/useTasks.tsx b/src/hooks/useTasks.tsx index 6e71f47..3f90afe 100644 --- a/src/hooks/useTasks.tsx +++ b/src/hooks/useTasks.tsx @@ -1,4 +1,4 @@ -import { useEffect, useState } from "react"; +import { useCallback, useEffect, useState } from "react"; import type { Task } from "../types/task"; export const useTasks = () => { @@ -26,15 +26,15 @@ export const useTasks = () => { setTasks(prev => [...prev, newTask]); }; - const deleteTask = (id: number) => { + const deleteTask = useCallback((id: number) => { setTasks(tasks.filter(task => task.id !== id)); - } + }, []); - const toggleTask = (id: number) => { + const toggleTask = useCallback((id: number) => { setTasks(tasks.map(task => task.id === id ? { ...task, completed: !task.completed } : task )); - - } + }, []); + return { tasks, addTask, deleteTask, toggleTask }; }; \ No newline at end of file