帮我用React 写个todolist
- User
帮我用React 写个todolist
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
帮我用React 写个todolist
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
我来帮你用React写一个todolist应用:
import React, { useState } from 'react';
import './TodoList.css';
const TodoList = () => {
const [todos, setTodos] = useState([]);
const [inputValue, setInputValue] = useState('');
// 添加新的todo
const addTodo = () => {
if (inputValue.trim() !== '') {
const newTodo = {
id: Date.now(),
text: inputValue,
completed: false
};
setTodos([...todos, newTodo]);
setInputValue('');
}
};
// 切换todo完成状态
const toggleTodo = (id) => {
setTodos(todos.map(todo =>
todo.id === id ? { ...todo, completed: !todo.completed } : todo
));
};
// 删除todo
const deleteTodo = (id) => {
setTodos(todos.filter(todo => todo.id !== id));
};
// 处理回车键
const handleKeyPress = (e) => {
if (e.key === 'Enter') {
addTodo();
}
};
return (
<div className="todo-container">
<h1>我的待办清单</h1>
<div className="input-section">
<input
type="text"
value={inputValue}
onChange={(e) => setInputValue(e.target.value)}
onKeyPress={handleKeyPress}
placeholder="添加新任务..."
className="todo-input"
/>
<button onClick={addTodo} className="add-button">
添加
</button>
</div>
<ul className="todo-list">
{todos.map(todo => (
<li key={todo.id} className={`todo-item ${todo.completed ? 'completed' : ''}`}>
<span
onClick={() => toggleTodo(todo.id)}
className="todo-text"
>
{todo.text}
</span>
<button
onClick={() => deleteTodo(todo.id)}
className="delete-button"
>
删除
</button>
</li>
))}
</ul>
{todos.length === 0 && (
<p className="empty-message">暂无任务,添加一个开始吧!</p>
)}
</div>
);
};
export default TodoList;
配套的CSS样式文件 (TodoList.css):
.todo-container {
max-width: 500px;
margin: 0 auto;
padding: 20px;
font-family: Arial, sans-serif;
}
h1 {
text-align: center;
color: #333;
margin-bottom: 30px;
}
.input-section {
display: flex;
margin-bottom: 20px;
gap: 10px;
}
.todo-input {
flex: 1;
padding: 10px;
border: 2px solid #ddd;
border-radius: 4px;
font-size: 16px;
}
.todo-input:focus {
outline: none;
border-color: #007bff;
}
.add-button {
padding: 10px 20px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
.add-button:hover {
background-color: #0056b3;
}
.todo-list {
list-style: none;
padding: 0;
}
.todo-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 12px;
margin-bottom: 8px;
background-color: #f8f9fa;
border-radius: 4px;
border-left: 4px solid #007bff;
}
.todo-item.completed {
background-color: #e9ecef;
border-left-color: #28a745;
}
.todo-text {
flex: 1;
cursor: pointer;
font-size: 16px;
}
.todo-item.completed .todo-text {
text-decoration: line-through;
color: #6c757d;
}
.delete-button {
padding: 5px 10px;
background-color: #dc3545;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
}
.delete-button:hover {
background-color: #c82333;
}
.empty-message {
text-align: center;
color: #6c757d;
font-style: italic;
margin-top: 30px;
}
这个todolist包含以下功能:
主要功能:
技术特点:
使用时只需要在你的主组件中导入并使用:
import TodoList from './TodoList';
function App() {
return (
<div className="App">
<TodoList />
</div>
);
}
这个todolist简洁实用,你可以根据需要添加更多功能,比如编辑任务、筛选功能等。