Todo List App in React
Below is a simple implementation of a Todo List app in React. This example is contained within a single file.
import React, { useState } from 'react';
// Todo List App Component
function TodoListApp() {
// State to store todos
const [todos, setTodos] = useState([]);
// State to store the current todo input
const [newTodo, setNewTodo] = useState('');
// Function to add a new todo
const addTodo = () => {
if (newTodo.trim() !== '') {
setTodos([...todos, { text: newTodo, completed: false }]);
setNewTodo('');
}
};
// Function to toggle the completion status of a todo
const toggleTodo = (index) => {
const updatedTodos = [...todos];
updatedTodos[index].completed = !updatedTodos[index].completed;
setTodos(updatedTodos);
};
// Function to remove a todo
const removeTodo = (index) => {
const updatedTodos = [...todos];
updatedTodos.splice(index, 1);
setTodos(updatedTodos);
};
return (
<div style={{ width: '300px', margin: '40px auto', padding: '20px', backgroundColor: '#f9f9f9', border: '1px solid #ddd', borderRadius: '10px', boxShadow: '0 0 10px rgba(0,0,0,0.1)' }}>
<h2>Todo List</h2>
<input
type="text"
value={newTodo}
onChange={(e) => setNewTodo(e.target.value)}
placeholder="Add new todo"
style={{ width: '100%', padding: '10px', marginBottom: '10px', border: '1px solid #ccc' }}
/>
<button onClick={addTodo} style={{ width: '100%', padding: '10px', backgroundColor: '#4CAF50', color: '#fff', border: 'none', borderRadius: '5px', cursor: 'pointer' }}>
Add Todo
</button>
<ul style={{ listStyle: 'none', padding: 0, margin: 0 }}>
{todos.map((todo, index) => (
<li key={index} style={{ padding: '10px', borderBottom: '1px solid #ccc' }}>
<input
type="checkbox"
checked={todo.completed}
onChange={() => toggleTodo(index)}
style={{ marginRight: '10px' }}
/>
<span style={{ textDecoration: todo.completed ? 'line-through' : 'none' }}>{todo.text}</span>
<button onClick={() => removeTodo(index)} style={{ float: 'right', backgroundColor: '#f44336', color: '#fff', border: 'none', padding: '5px 10px', borderRadius: '5px', cursor: 'pointer' }}>
Remove
</button>
</li>
))}
</ul>
</div>
);
}
// Render the Todo List App
const root = document.getElementById('root');
if (root) {
const ReactRoot = React.createRoot(root);
ReactRoot.render(<TodoListApp />);
} else {
console.error('Failed to find the root element');
}
To run this code, create an HTML file (e.g., index.html) with a div element having an id of root, and include the React and ReactDOM libraries. You can use a CDN for simplicity.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Todo List App</title>
</head>
<body>
<div id="root"></div>
<script src="https://unpkg.com/react@18/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js" crossorigin></script>
<script src="app.js"></script> <!-- Assuming you saved the React code in a file named app.js -->
</body>
</html>
Make sure to replace app.js with the actual path to your JavaScript file containing the React code.
This example demonstrates a basic Todo List app with the ability to add, toggle completion, and remove todos.