In this guide, we'll create a simple React application that allows users to manage a list of tasks. This application will include functionality to add, delete, and mark tasks as completed. We'll use functional components and React hooks to achieve this.
First, ensure you have Node.js installed on your machine. Then, create a new React application using Create React App:
npx create-react-app task-manager
Navigate into your new project directory:
cd task-manager
Open your project in your favorite code editor. In the src
folder, create a new file called TaskManager.js
. This file will contain our main component.
import React, { useState } from 'react';
const TaskManager = () => {
const [tasks, setTasks] = useState([]);
const [taskInput, setTaskInput] = useState('');
const addTask = (e) => {
e.preventDefault();
if (taskInput.trim() !== '') {
setTasks([...tasks, { text: taskInput, completed: false }]);
setTaskInput('');
}
};
const deleteTask = (index) => {
const newTasks = tasks.filter((_, i) => i !== index);
setTasks(newTasks);
};
const toggleTaskCompletion = (index) => {
const newTasks = tasks.map((task, i) => {
if (i === index) {
return { ...task, completed: !task.completed };
}
return task;
});
setTasks(newTasks);
};
return (
<div>
<h1>Task Manager</h1>
<form onSubmit={addTask}>
<input
type="text"
value={taskInput}
onChange={(e) => setTaskInput(e.target.value)}
placeholder="Add a new task"
/>
<button type="submit">Add Task</button>
</form>
<ul>
{tasks.map((task, index) => (
<li key={index} style={{ textDecoration: task.completed ? 'line-through' : 'none' }}>
{task.text}
<button onClick={() => toggleTaskCompletion(index)}>{task.completed ? 'Undo' : 'Complete'}</button>
<button onClick={() => deleteTask(index)}>Delete</button>
</li>
))}</ul>
</div>
);
};
export default TaskManager;
Now, let's integrate the TaskManager
component into our main application. Open src/App.js
and modify it as follows:
import React from 'react';
import TaskManager from './TaskManager';
const App = () => {
return (
<div className="App">
<TaskManager />
</div>
);
};
export default App;
Now that we've set up our application, it's time to run it. In your terminal, execute:
npm start
This command will start the development server, and you should be able to see your Task Manager application in your browser at http://localhost:3000
.
You can add some basic CSS to make your application look better. Create a new file src/App.css
and add the following styles:
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 20px;
}
.App {
max-width: 600px;
margin: auto;
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
h1 {
text-align: center;
}
form {
display: flex;
justify-content: space-between;
}
input {
flex: 1;
padding: 10px;
margin-right: 10px;
}
button {
padding: 10px;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: flex;
justify-content: space-between;
padding: 10px 0;
}
Don’t forget to import the CSS file in src/App.js
:
import './App.css';
You have now created a simple task manager application using React! This application demonstrates the use of state management with hooks, event handling, and rendering lists in React. You can further enhance this application by adding features such as editing tasks, storing tasks in local storage, or even integrating a backend API.
For more information on React, you can visit the official React documentation.
© 2025 Invastor. All Rights Reserved
User Comments