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
Create a React component called
FoodListthat allows users to add, save, and delete food items with their corresponding details. The component should have the following features:- A header with the text "Food Items List".
- A button labeled "Add Food" that, when clicked, displays a form to add a new food item. The form should include the following fields: a. Item Name (text input) b. Food Type (text input) c. Spiciness Level (number input with a range of 1-10)
- A "Save" button that becomes visible when the "Add Food" button is clicked. Clicking the "Save" button should add the new food item to the list and clear the form.
- A list of added food items displaying their item name, food type, and spiciness level.
- A "Delete" button next to each food item in the list that, when clicked, removes the corresponding food item from the list.
Here's the solution code for the
FoodListcomponent:jsximport React, { useState } from "react"; import "../App.css"; function FoodList() { // ... (The solution code you provided) } export default FoodList;
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.