-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathApp.js
More file actions
52 lines (45 loc) · 1.41 KB
/
App.js
File metadata and controls
52 lines (45 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import React, { useState } from "react";
function App() {
// State to manage tasks
const [tasks, setTasks] = useState([]);
const [newTask, setNewTask] = useState("");
// Add task function
const addTask = () => {
if (newTask.trim() === "") return;
setTasks([...tasks, newTask]);
setNewTask("");
};
// Remove task function
const removeTask = (index) => {
const updatedTasks = tasks.filter((_, i) => i !== index);
setTasks(updatedTasks);
};
return (
<div className="min-h-screen flex flex-col items-center justify-center bg-gray-100 p-4">
<h1 className="text-2xl font-bold mb-4">To-Do List</h1>
{/* Input & Add Button */}
<div className="flex gap-2">
<input
type="text"
className="border p-2 rounded"
placeholder="Enter task..."
value={newTask}
onChange={(e) => setNewTask(e.target.value)}
/>
<button className="bg-blue-500 text-white px-4 py-2 rounded" onClick={addTask}>
Add Task
</button>
</div>
{/* Task List */}
<ul className="mt-4 w-64">
{tasks.map((task, index) => (
<li key={index} className="flex justify-between bg-white p-2 mb-2 shadow rounded">
{task}
<button className="text-red-500" onClick={() => removeTask(index)}>❌</button>
</li>
))}
</ul>
</div>
);
}
export default App;