React Todo List App (Add + Delete using useState)
๐ INTRODUCTION
The Todo List App is one of the most popular beginner projects in React. It helps you understand:
- State management
- List rendering
- Event handling
In this program, we will build a Todo app that:
- Adds tasks
- Deletes tasks
- Displays task list dynamically
๐ This is a must-do project for beginners.
๐ PROBLEM STATEMENT
๐ Create a Todo List App in React that allows users to add and delete tasks using useState.
๐ก CONCEPTS USED
- useState Hook
- List Rendering (
map()) - Event Handling
- Controlled Input
๐ง LOGIC EXPLANATION
- Store tasks in an array
- Take input from user
- Add task to list
- Delete task from list
- Render tasks dynamically
๐ป PROGRAM CODE
Open src/App.js:
import { useState } from "react";
function App() {
const [task, setTask] = useState("");
const [tasks, setTasks] = useState([]);
const handleAdd = () => {
if (task.trim() === "") return;
setTasks([...tasks, task]);
setTask("");
};
const handleDelete = (index) => {
const newTasks = tasks.filter((_, i) => i !== index);
setTasks(newTasks);
};
return (
<div style={styles.container}>
<h1>Todo List</h1>
<input
type="text"
value={task}
placeholder="Enter task"
onChange={(e) => setTask(e.target.value)}
/>
<button onClick={handleAdd}>Add</button>
<ul>
{tasks.map((t, index) => (
<li key={index}>
{t}
<button onClick={() => handleDelete(index)}>Delete</button>
</li>
))}
</ul>
</div>
);
}
const styles = {
container: {
textAlign: "center",
marginTop: "50px"
}
};
export default App;
๐ฅ๏ธ OUTPUT
๐ Example:
Input: Learn React
Click Add
Output:
- Learn React [Delete]
๐ After delete:
(No tasks)
๐ STEP-BY-STEP EXPLANATION
useState("")โ stores input valueuseState([])โ stores list of taskshandleAdd()โ adds task to arrayhandleDelete()โ removes taskmap()โ displays tasks
โก ADVANCED FEATURES
1. Add Task IDs
{ id: Date.now(), text: task }
2. Add Edit Option
๐ Modify task using index
3. Add Completed Toggle
๐ Strike-through completed tasks
4. Save to Local Storage
localStorage.setItem("tasks", JSON.stringify(tasks));
๐ฏ WHY THIS PROGRAM IS IMPORTANT
- Real-world project foundation
- Combines multiple React concepts
- Frequently asked in interviews
- Helps in portfolio building
๐ REAL-WORLD USE CASE
- Task management apps
- Daily planners
- Productivity tools
- Notes apps
โ VIVA QUESTIONS (IMPORTANT)
Q1. What is useState?
๐ A hook to manage state in React.
Q2. Why use map()?
๐ To display list items dynamically.
Q3. What is controlled input?
๐ Input controlled by React state.
Q4. Why use key in list?
๐ To uniquely identify elements.
Q5. How to delete an item?
๐ Using filter() method.
๐ RELATED POSTS
๐ MCA 168 Full Stack Development Lab Programs List
๐ React Calculator App โ Program 17
๐ React Todo Item โ Program 12
๐ React useState โ Program 6
๐ CONCLUSION
The Todo List App is a powerful beginner project that demonstrates how React handles state and dynamic UI updates.
๐ Build this and extend it to create full applications.
Further Reading
How to Master Full Stack Development?
Spring Framework Practice Problems and Their Solutions
From Google to the World: The Story of Go Programming Language
Why Go? Understanding the Advantages of this Emerging Language
Creating and Executing Simple Programs in Go
20+ Interview Questions on Go Programming Language
100+ MCQs On Java Architecture
- Angular
- ASP.NET
- C
- C#
- C++
- CSS
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- PHP
- Power Bi
- Python
- Scratch 3.0
- TypeScript
- VB.NET
