Here's a complete todo list app in React as a single file:
import React, { useState } from 'react';
function TodoApp() {
const [todos, setTodos] = useState([]);
const [inputValue, setInputValue] = useState('');
const addTodo = () => {
if (inputValue.trim() !== '') {
setTodos([...todos, {
id: Date.now(),
text: inputValue.trim(),
completed: false
}]);
setInputValue('');
}
};
const deleteTodo = (id) => {
setTodos(todos.filter(todo => todo.id !== id));
};
const toggleComplete = (id) => {
setTodos(todos.map(todo =>
todo.id === id ? { ...todo, completed: !todo.completed } : todo
));
};
const handleKeyPress = (e) => {
if (e.key === 'Enter') {
addTodo();
}
};
return (
<div style={{
maxWidth: '500px',
margin: '50px auto',
padding: '20px',
fontFamily: 'Arial, sans-serif'
}}>
<h1 style={{ textAlign: 'center', color: '#333' }}>Todo List</h1>
<div style={{ marginBottom: '20px', display: 'flex', gap: '10px' }}>
<input
type="text"
value={inputValue}
onChange={(e) => setInputValue(e.target.value)}
onKeyPress={handleKeyPress}
placeholder="Add a new todo..."
style={{
flex: 1,
padding: '10px',
border: '1px solid #ddd',
borderRadius: '4px',
fontSize: '16px'
}}
/>
<button
onClick={addTodo}
style={{
padding: '10px 20px',
backgroundColor: '#007bff',
color: 'white',
border: 'none',
borderRadius: '4px',
cursor: 'pointer',
fontSize: '16px'
}}
>
Add
</button>
</div>
<div>
{todos.length === 0 ? (
<p style={{ textAlign: 'center', color: '#666', fontStyle: 'italic' }}>
No todos yet. Add one above!
</p>
) : (
todos.map(todo => (
<div
key={todo.id}
style={{
display: 'flex',
alignItems: 'center',
padding: '10px',
margin: '5px 0',
backgroundColor: '#f8f9fa',
borderRadius: '4px',
border: '1px solid #e9ecef'
}}
>
<input
type="checkbox"
checked={todo.completed}
onChange={() => toggleComplete(todo.id)}
style={{ marginRight: '10px' }}
/>
<span
style={{
flex: 1,
textDecoration: todo.completed ? 'line-through' : 'none',
color: todo.completed ? '#666' : '#333'
}}
>
{todo.text}
</span>
<button
onClick={() => deleteTodo(todo.id)}
style={{
padding: '5px 10px',
backgroundColor: '#dc3545',
color: 'white',
border: 'none',
borderRadius: '4px',
cursor: 'pointer',
fontSize: '14px'
}}
>
Delete
</button>
</div>
))
)}
</div>
{todos.length > 0 && (
<div style={{ marginTop: '20px', textAlign: 'center', color: '#666' }}>
Total: {todos.length} | Completed: {todos.filter(t => t.completed).length}
</div>
)}
</div>
);
}
export default TodoApp;
This todo app includes:
- Add new todos with the input field and "Add" button (or press Enter)
- Mark todos as complete/incomplete with checkboxes
- Delete individual todos with the delete button
- Visual feedback for completed items (strikethrough text)
- A counter showing total and completed todos
- Clean, responsive styling with inline CSS
- Empty state message when no todos exist
The app uses React hooks (useState) to manage the todo list state and input field, and includes all the core functionality you'd expect from a todo list application.