forked from MTalhaofc/HacktoberFest-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ToDoList.js
49 lines (43 loc) · 1.47 KB
/
ToDoList.js
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
import React, { useState } from 'react';
const TodoApp = () => {
const [tasks, setTasks] = useState([]);
const [taskInput, setTaskInput] = useState('');
const addTask = () => {
if (taskInput) {
setTasks([...tasks, { text: taskInput, completed: false }]);
setTaskInput('');
}
};
const toggleTaskCompletion = (index) => {
const newTasks = tasks.map((task, i) =>
i === index ? { ...task, completed: !task.completed } : task
);
setTasks(newTasks);
};
const deleteTask = (index) => {
const newTasks = tasks.filter((_, i) => i !== index);
setTasks(newTasks);
};
return (
<div>
<h1>To-Do List</h1>
<input
type="text"
value={taskInput}
onChange={(e) => setTaskInput(e.target.value)}
placeholder="Add a new task"
/>
<button onClick={addTask}>Add Task</button>
<ul>
{tasks.map((task, index) => (
<li key={index} style={{ textDecoration: task.completed ? 'line-through' : 'none' }}>
{task.text}
<button onClick={() => toggleTaskCompletion(index)}>Toggle</button>
<button onClick={() => deleteTask(index)}>Delete</button>
</li>
))}
</ul>
</div>
);
};
export default TodoApp;