React Project - Build a Complete Todo App from Scratch with Advanced Features

React Project - Build a Complete Todo App from Scratch with Advanced Features

In this guide, we will build a fully functional Todo App using React, covering state management, event handling, and advanced features like filtering and local storage.

Key Features

- Add, edit, and delete tasks.
- Mark tasks as completed.
- Filter tasks (All, Completed, Pending).
- Store tasks in local storage for persistence.

Installation and Setup

    
// Create a new React project
npx create-react-app todo-app
cd todo-app
npm start
    
  

Todo App Component

    
import React, { useState, useEffect } from "react";

function TodoApp() {
  const [tasks, setTasks] = useState([]);
  const [newTask, setNewTask] = useState("");

  useEffect(() => {
    const storedTasks = JSON.parse(localStorage.getItem("tasks")) || [];
    setTasks(storedTasks);
  }, []);

  useEffect(() => {
    localStorage.setItem("tasks", JSON.stringify(tasks));
  }, [tasks]);

  const addTask = () => {
    if (newTask.trim() === "") return;
    setTasks([...tasks, { text: newTask, completed: false }]);
    setNewTask("");
  };

  const toggleTask = (index) => {
    const updatedTasks = tasks.map((task, i) =>
      i === index ? { ...task, completed: !task.completed } : task
    );
    setTasks(updatedTasks);
  };

  return (
    

Todo List

setNewTask(e.target.value)} className="border p-2 w-full mb-2" />
    {tasks.map((task, index) => (
  • toggleTask(index)} className={`p-2 border rounded-md cursor-pointer ${task.completed ? 'line-through text-gray-500' : ''}`} > {task.text}
  • ))}
); } export default TodoApp;

Challenge: Add More Features

- Implement a task editing feature.
- Add categories or priorities to tasks.
- Improve UI with animations and better styling.