create a react assignment question which has solution given below import React, {…
- User
create a react assignment question which has solution given below
import React, { useState } from "react"; import "../App.css"; function FoodList() { const [foods, setFoods] = useState([]); const [itemName, setItemName] = useState(""); const [foodType, setFoodType] = useState(""); const [spicinessLevel, setSpicinessLevel] = useState(""); const [isFirstCardEnabled, setIsFirstCardEnabled] = useState(false); const [isSecondCardEnabled, setIsSecondCardEnabled] = useState(false); const [isFormEnabled, setIsFormEnabled] = useState(false);
codeconst handleFormClick = () => { setIsFormEnabled(true); }; const handleAddFood = () => { setIsFirstCardEnabled(true); setIsSecondCardEnabled(true); }; const handleSaveFood = () => { const newFood = { itemName, foodType, spicinessLevel, }; setFoods([...foods, newFood]); setItemName(""); setFoodType(""); setSpicinessLevel(""); setIsFirstCardEnabled(false); setIsSecondCardEnabled(false); setIsFormEnabled(false); }; const handleDeleteFood = (index) => { const newFoods = [...foods]; newFoods.splice(index, 1); setFoods(newFoods); }; return ( <> <div> <h1>Food Items List</h1> Add Food <div> {isFirstCardEnabled ? ( <> <h2>Item Name:</h2> setItemName(e.target.value)} disabled={!isFirstCardEnabled} /> <h2>Food Type:</h2> setFoodType(e.target.value)} disabled={!isFirstCardEnabled} /> <div> e.preventDefault()} onClick={handleFormClick}> <h2>Spiciness Level:</h2> setSpicinessLevel(e.target.value)} /> </div> </> ) : ( "" )} </div> {isFirstCardEnabled ? ( <div> Save </div> ) : ( "" )} <ul> {foods.map((food, index) => ( <li> {food.itemName} ({food.foodType}) - Spiciness Level:{" "} {food.spicinessLevel} handleDeleteFood(index)}>Delete </li> ))} </ul> </div> </> ); - Assistant
Assignment:
Create a React application that allows users to add food items to a list. Each food item should have a name, type (e.g. vegetable, fruit, etc.), and spiciness level. The application should have the following functionality:
- Display a "Add Food" button that, when clicked, enables the user to input the name, type, and spiciness level of the food item.
- When the user clicks the "Save" button, the food item should be added to the list and the input fields should be cleared.
- The list should display the name, type, and spiciness level of each food item.
- Each food item in the list should have a "Delete" button that removes the item from the list.
Solution:
The solution for this assignment is provided in the code snippet you provided. It includes a functional component called that uses React's hook to manage the state of the food items list, input fields, and button states.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.