code me out a react app for todo
- User
code me out a react app for todo
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
code me out a react app for todo
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
import React, { useState, useEffect } from "react";
import "./App.css";
function TodoApp() {
const [todos, setTodos] = useState(() => {
// Load todos from localStorage if available
const savedTodos = localStorage.getItem("todos");
return savedTodos ? JSON.parse(savedTodos) : [];
});
const [inputValue, setInputValue] = useState("");
const [filter, setFilter] = useState("all");
// Save todos to localStorage whenever they change
useEffect(() => {
localStorage.setItem("todos", JSON.stringify(todos));
}, [todos]);
// Add a new todo
const handleAddTodo = (e) => {
e.preventDefault();
if (inputValue.trim() === "") return;
const newTodo = {
id: Date.now(),
text: inputValue,
completed: false
};
setTodos([...todos, newTodo]);
setInputValue("");
};
// Toggle todo completion status
const handleToggleTodo = (id) => {
setTodos(
todos.map(todo =>
todo.id === id ? { ...todo, completed: !todo.completed } : todo
)
);
};
// Delete a todo
const handleDeleteTodo = (id) => {
setTodos(todos.filter(todo => todo.id !== id));
};
// Filter todos based on current filter
const filteredTodos = todos.filter(todo => {
if (filter === "active") return !todo.completed;
if (filter === "completed") return todo.completed;
return true;
});
return (
<div className="todo-app">
<h1>Todo App</h1>
<form onSubmit={handleAddTodo} className="todo-form">
<input
type="text"
value={inputValue}
onChange={(e) => setInputValue(e.target.value)}
placeholder="Add a new task..."
className="todo-input"
/>
<button type="submit" className="add-button">Add</button>
</form>
<div className="filters">
<button
className={filter === "all" ? "active" : ""}
onClick={() => setFilter("all")}
>
All
</button>
<button
className={filter === "active" ? "active" : ""}
onClick={() => setFilter("active")}
>
Active
</button>
<button
className={filter === "completed" ? "active" : ""}
onClick={() => setFilter("completed")}
>
Completed
</button>
</div>
<ul className="todo-list">
{filteredTodos.length === 0 ? (
<li className="empty-message">No tasks found</li>
) : (
filteredTodos.map(todo => (
<li key={todo.id} className="todo-item">
<div className="todo-content">
<input
type="checkbox"
checked={todo.completed}
onChange={() => handleToggleTodo(todo.id)}
/>
<span
className={todo.completed ? "completed" : ""}
>
{todo.text}
</span>
</div>
<button
onClick={() => handleDeleteTodo(todo.id)}
className="delete-button"
>
Delete
</button>
</li>
))
)}
</ul>
<div className="todo-stats">
<p>{todos.filter(todo => !todo.completed).length} tasks remaining</p>
</div>
</div>
);
}
// Add this to index.js or App.js depending on your setup
export default function App() {
return (
<div className="App">
<TodoApp />
</div>
);
}
And here's some basic CSS to style the app:
/* App.css */
.todo-app {
max-width: 500px;
margin: 0 auto;
padding: 20px;
font-family: Arial, sans-serif;
}
h1 {
text-align: center;
color: #333;
}
.todo-form {
display: flex;
margin-bottom: 20px;
}
.todo-input {
flex: 1;
padding: 10px;
font-size: 16px;
border: 1px solid #ddd;
border-radius: 4px 0 0 4px;
}
.add-button {
padding: 10px 15px;
background-color: #4caf50;
color: white;
border: none;
border-radius: 0 4px 4px 0;
cursor: pointer;
font-size: 16px;
}
.filters {
display: flex;
justify-content: center;
margin-bottom: 20px;
}
.filters button {
margin: 0 5px;
padding: 8px 12px;
background: none;
border: 1px solid #ddd;
border-radius: 4px;
cursor: pointer;
}
.filters button.active {
background-color: #f0f0f0;
border-color: #999;
}
.todo-list {
list-style-type: none;
padding: 0;
}
.todo-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px;
margin-bottom: 10px;
background-color: #f9f9f9;
border-radius: 4px;
}
.todo-content {
display: flex;
align-items: center;
}
.todo-content input {
margin-right: 10px;
}
.completed {
text-decoration: line-through;
color: #888;
}
.delete-button {
padding: 5px 10px;
background-color: #f44336;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
.todo-stats {
text-align: center;
color: #666;
font-size: 14px;
}
.empty-message {
text-align: center;
color: #888;
padding: 10px;
}
Would you like me to explain or break down the code?