How to create next js todo application
- User
How to create next js todo application
Assistant
Creating a Next.js Todo Application
Here's how to build a simple todo app with Next.js:
- First, create a new Next.js project:
npx create-next-app@latest todo-app cd todo-app- Create the main todo component in
app/page.js:
"use client"; import { useState } from 'react'; export default function TodoApp() { const [todos, setTodos] = useState([]); const [input, setInput] = useState(''); const addTodo = () => { if (input.trim()) { setTodos([...todos, { id: Date.now(), text: input, completed: false }]); setInput(''); } }; const toggleTodo = (id) => { setTodos(todos.map(todo => todo.id === id ? { ...todo, completed: !todo.completed } : todo )); }; const deleteTodo = (id) => { setTodos(todos.filter(todo => todo.id !== id)); }; return ( <div className="max-w-md mx-auto mt-10 p-6 bg-white rounded-lg shadow-md"> <h1 className="text-2xl font-bold mb-4">Todo App</h1> <div className="flex mb-4"> <input type="text" value={input} onChange={(e) => setInput(e.target.value)} onKeyPress={(e) => e.key === 'Enter' && addTodo()} className="flex-grow p-2 border border-gray-300 rounded-l" placeholder="Add a task..." /> <button onClick={addTodo} className="bg-blue-500 text-white px-4 py-2 rounded-r" > Add </button> </div> <ul> {todos.map(todo => ( <li key={todo.id} className="flex items-center p-2 border-b"> <input type="checkbox" checked={todo.completed} onChange={() => toggleTodo(todo.id)} className="mr-2" /> <span className={todo.completed ? 'line-through flex-grow' : 'flex-grow'}> {todo.text} </span> <button onClick={() => deleteTodo(todo.id)} className="text-red-500" > Delete </button> </li> ))} </ul> </div> ); }- Start your development server:
npm run devWould you like me to explain or break down the code?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.