You are a senior level frontend React engineer. I want you to write an application that…
- User
You are a senior level frontend React engineer. I want you to write an application that will create a list of todos
- Assistant
As a senior level frontend React engineer, I will create a simple yet functional todo list application using React. This application will allow users to add, view, and delete todos. I will use functional components with React hooks for state management.
Here's the code for the todo list application:
jsximport React, { useState } from 'react'; function TodoList() { const [todos, setTodos] = useState([]); const [input, setInput] = useState(''); const addTodo = (e) => { e.preventDefault(); if (input.trim() !== '') { setTodos([...todos, { id: Date.now(), text: input }]); setInput(''); } }; const deleteTodo = (id) => { setTodos(todos.filter((todo) => todo.id !== id)); }; return ( <div> <h1>Todo List</h1> <form onSubmit={addTodo}> <input type="text" value={input} onChange={(e) => setInput(e.target.value)} /> <button type="submit">Add Todo</button> </form> <ul> {todos.map((todo) => ( <li key={todo.id}> {todo.text} <button onClick={() => deleteTodo(todo.id)}>Delete</button> </li> ))} </ul> </div> ); } export default TodoList;This code creates a functional component called
TodoListthat manages the state of the todos and the input field. Thetodosstate is an array of todo objects, each with anidandtextproperty. Theinputstate holds the current value of the input field.The
addTodofunction is called when the form is submitted. It prevents the default form submission behavior, checks if the input is not empty, and adds a new todo object to thetodosarray.The
deleteTodofunction is called when the delete button is clicked. It filters thetodosarray to remove the todo with the specifiedid.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.