Create a React application that manages a list of food items. The app should allow users to add, save, and delete food items with specific details. Your solution will be evaluated based on functionality, code organization, and adherence to best practices.
Requirements:
Initial State:
The app displays an "Add Food" button.
The food list is empty.
Adding Food:
Upon clicking "Add Food", a form with the following fields should appear:
Item Name (text input)
Food Type (text input)
Spiciness Level (text input)
A "Save" button should be visible when the form is displayed.
Saving Food:
When "Save" is clicked, the new food item should be added to the list.
The form fields should be cleared.
The form and "Save" button should disappear after saving.
Food List Display:
Each food item in the list should display:
Item Name
Food Type (in parentheses)
Spiciness Level
A "Delete" button should be available for each food item.
Deleting Food:
Clicking "Delete" should remove the corresponding food item from the list.
Starting Point (Optional):
You can start with the provided code snippet below, which includes some initial state and functionality. However, feel free to implement the solution from scratch if you prefer. If you choose to use this snippet, please highlight the changes you made in your submission.
Provided Code Snippet (Optional Starting Point):
jsx
import React, { useState } from "react";
function FoodList() {
// Implement the required functionality
const [foods, setFoods] = useState([]); // List of food items
const [itemName, setItemName] = useState(""); // Input state for item name
const [foodType, setFoodType] = useState(""); // Input state for food type
const [spicinessLevel, setSpicinessLevel] = useState(""); // Input state for spiciness level
const [isFormEnabled, setIsFormEnabled] = useState(false); // Toggle form visibility
// Handle form visibility
const handleFormClick = () => {
setIsFormEnabled(true);
};
// Save a new food item
const handleSaveFood = () => {
const newFood = { itemName, foodType, spicinessLevel };
setFoods([...foods, newFood]);
setItemName("");
setFoodType("");
setSpicinessLevel("");
setIsFormEnabled(false);
};
// Remove a food item by index
const handleDeleteFood = (index) => {
const newFoods = [...foods];
newFoods.splice(index, 1);
setFoods(newFoods);
};
return (
<div>
<h1>Food Items List</h1>
{/* Toggle form visibility */}
<button onClick={handleFormClick}>Add Food</button>
{/* Render form if enabled */}
{isFormEnabled && (
<div>
<input
type="text"
value={itemName}
onChange={(e) => setItemName(e.target.value)}
placeholder="Item Name"
/>
<input
type="text"
value={foodType}
onChange={(e) => setFoodType(e.target.value)}
placeholder="Food Type"
/>
<input
type="text"
value={spicinessLevel}
onChange={(e) => setSpicinessLevel(e.target.value)}
placeholder="Spiciness Level"
/>
{/* Save button */}
<button onClick={handleSaveFood}>Save</button>
</div>
)}
{/* Display food list */}
<ul>
{foods.map((food, index) => (
<li key={index}>
{food.itemName} ({food.foodType}) - Spiciness Level: {food.spicinessLevel}
{/* Delete button for each item */}
<button onClick={() => handleDeleteFood(index)}>Delete</button>
</li>
))}
</ul>
</div>
);
}
export default FoodList;
Guidance on Implementation:
Use the useState hook for state management.
Implement event handlers for form submission, saving, and deleting items.
Conditionally render the form and list based on state.
Ensure each list item has a unique key (e.g., index) for React.
Best Practices to Follow:
Use meaningful variable and function names.
Keep JSX organized and easy to read.
Avoid unnecessary re-renders by optimizing state updates.
Use comments to explain key parts of your code.
Submission Guidelines:
Provide the complete FoodList.js file with your implementation.
Ensure the code is well-organized, readable, and follows best practices.
If you used the provided code snippet, highlight the changes you made.
Include a brief explanation (max. 100 words) of your design decisions and any challenges you faced.
Evaluation Criteria:
Functionality (40%): Does the app meet all the requirements?
Code Quality (30%): Is the code well-organized, readable, and maintainable?
Best Practices (20%): Are React best practices followed (e.g., state management, JSX structure)?
Design Decisions and Explanation (10%): Are the design choices clear, and is the explanation concise?