React Todo
React Todo
Solution :
Our first task is to set up the React project. This step involves creating the necessary
project structure. Here's how you can do it:
Open your terminal and navigate to your preferred directory. Run the following command
to generate a new React app. Replace "todo-app" with your desired project name:
This command will create a directory named "todo-app" with all the initial code required
for a React app.
cd todo-app
npm start
This will open your React app, and you�ll see the default React starter page in your web
browser at 'http://localhost:3000'.
In this step, we create the App component, which serves as the entry point to our Todo
List application.
src->Component
Now, let's create the 'TodoList' component, which is responsible for managing the list of
tasks and handling task-related functionality.
function TodoList() {
const [tasks, setTasks] = useState([
{
id: 1,
text: 'Doctor Appointment',
completed: true
},
{
id: 2,
text: 'Meeting at School',
completed: false
}
]);
src->Component
In this step, we create the 'TodoItem' component, which represents an individual task in
our Todo List.
return (
<div className="todo-item">
<input
type="checkbox"
checked={task.completed}
onChange={handleChange}
/>
<p>{task.text}</p>
<button onClick={() => deleteTask(task.id)}>
X
</button>
</div>
);
}
exportdefault TodoItem;
These three components, 'App', 'TodoList', and 'TodoItem', work together to create a
functional Todo List application in React. The 'TodoList' component manages the state of
the tasks, and the 'TodoItem' component represents and handles individual tasks within
the list.
Step 5: Styling
To enhance the visual appeal of your Todo List, you can apply some basic styling.
Here�s an example of how you can style the todo items. In the `App.css` file, add the
following styles:
.todo-item {
display: flex;
justify-content: space-between;
margin-bottom: 8px;
}
.todo-itemp {
color: #888;
text-decoration: line-through;
}
Output :
Initially it looks like:
Next,