npx create-react-app todo-list
cd todo-list
npm start
import React, { useState } from 'react';
import './TodoList.css';
const TodoList = () => {
const [tasks, setTasks] = useState([]);
const [inputValue, setInputValue] = useState('');
const handleInputChange = (event) => {
setInputValue(event.target.value);
};
const addTask = () => {
if (inputValue.trim()) {
setTasks([...tasks, inputValue.trim()]);
setInputValue('');
}
};
const removeTask = (index) => {
const newTasks = tasks.filter((task, i) => i !== index);
setTasks(newTasks);
};
return (
<div className="todo-container">
<h1>To-Do List</h1>
<input
type="text"
value={inputValue}
onChange={handleInputChange}
placeholder="Add a new task"
/>
<button onClick={addTask}>Add</button>
<ul>
{tasks.map((task, index) => (
<li key={index}>
{task} <button onClick={() => removeTask(index)}>Remove</button>
</li>
))}
</ul>
</div>
);
};
export default TodoList;
<style>
.todo-container {
max-width: 400px;
margin: 50px auto;
padding: 20px;
border: 1px solid #ddd;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
text-align: center;
}
.todo-container h1 {
font-size: 24px;
margin-bottom: 20px;
}
.todo-container input {
width: 70%;
padding: 10px;
margin-right: 10px;
border: 1px solid #ddd;
border-radius: 4px;
}
.todo-container button {
padding: 10px 15px;
border: none;
background-color: #61dafb;
color: white;
border-radius: 4px;
cursor: pointer;
}
.todo-container ul {
list-style: none;
padding: 0;
margin-top: 20px;
}
.todo-container li {
margin-bottom: 10px;
display: flex;
justify-content: space-between;
align-items: center;
}
.todo-container li button {
padding: 5px 10px;
border: none;
background-color: red;
color: white;
border-radius: 4px;
cursor: pointer;
}
</style>
import React from 'react';
import TodoList from './TodoList';
const App = () => {
return (
<div className="App">
<TodoList />
</div>
);
};
export default App;