|
| 1 | +```js |
| 2 | +import React, { useState } from 'react'; |
| 3 | + |
| 4 | +export default function App() { |
| 5 | + const [tasks, setTasks] = useState([ |
| 6 | + 'Walk the dog', |
| 7 | + 'Water the plants', |
| 8 | + 'Wash the dishes', |
| 9 | + ]); // Initialize tasks with default items |
| 10 | + |
| 11 | + const [inputValue, setInputValue] = useState(''); // State for the input value |
| 12 | + |
| 13 | + // Handler to add a new task |
| 14 | + const handleSubmit = () => { |
| 15 | + if (inputValue.trim()) { |
| 16 | + setTasks([...tasks, inputValue.trim()]); // Add the new task |
| 17 | + setInputValue(''); // Clear the input field |
| 18 | + } |
| 19 | + }; |
| 20 | + |
| 21 | + // Handler to remove a task |
| 22 | + const handleDelete = (indexToRemove) => { |
| 23 | + setTasks(tasks.filter((_, index) => index !== indexToRemove)); // Remove the task by index |
| 24 | + }; |
| 25 | + |
| 26 | + return ( |
| 27 | + <div> |
| 28 | + <h1>Todo List</h1> |
| 29 | + <div> |
| 30 | + <input |
| 31 | + type="text" |
| 32 | + placeholder="Add your task" |
| 33 | + value={inputValue} // Bind value to state |
| 34 | + onChange={(e) => setInputValue(e.target.value)} // Update input state |
| 35 | + /> |
| 36 | + <div> |
| 37 | + <button onClick={handleSubmit}>Submit</button> {/* Add new task */} |
| 38 | + </div> |
| 39 | + </div> |
| 40 | + <ul> |
| 41 | + {tasks.map((task, index) => ( |
| 42 | + <li key={index}> |
| 43 | + <span>{task}</span> |
| 44 | + <button onClick={() => handleDelete(index)}>Delete</button> {/* Remove task */} |
| 45 | + </li> |
| 46 | + ))} |
| 47 | + </ul> |
| 48 | + </div> |
| 49 | + ); |
| 50 | +} |
| 51 | +``` |
| 52 | +### Explanation |
| 53 | +#### React State |
| 54 | +1. Tasks State: |
| 55 | + |
| 56 | +* The state tasks is initialized with a list of default tasks (['Walk the dog', 'Water the plants', 'Wash the dishes']). |
| 57 | +* Tasks are stored as an array, making it easy to add and remove items. |
| 58 | +2. Input State: |
| 59 | + |
| 60 | +* The state inputValue is used to track the value in the <input> field. |
| 61 | +* This state updates as the user types in the field and resets when a new task is added. |
| 62 | +#### Submit New Task |
| 63 | +The `handleSubmit` function adds a new task: |
| 64 | + |
| 65 | +* Validation: Checks if `inputValue.trim()` is not empty before adding the task. |
| 66 | +* State Update: |
| 67 | +* The setTasks function appends the new task (from the input) to the existing tasks array. |
| 68 | +* The setInputValue('') clears the input field. |
| 69 | +#### Delete Existing Task |
| 70 | +The `handleDelete` function removes a task: |
| 71 | + |
| 72 | +* Index-Based Removal: Uses the filter function to exclude the task at a specified index. |
| 73 | +#### State Update: |
| 74 | +* The setTasks function updates the state with the new list of tasks (excluding the removed task). |
| 75 | +#### User Interaction Flow |
| 76 | +1. Adding Tasks: |
| 77 | + |
| 78 | +* Type a task in the input box. |
| 79 | +* Click `"Submit"`. |
| 80 | +* The new task appears in the list, and the input field is cleared. |
| 81 | +2. Deleting Tasks: |
| 82 | + |
| 83 | +* Click the "Delete" button next to any task. |
| 84 | +* The task is removed from the list. |
0 commit comments