Should you use a single state object in a food ordering app?
Yes. Use one state object: { cart, searchQuery, filters }. All views read from this state. All actions update this state. This makes the app predictable and debuggable. Scattered state variables lead to bugs.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Food Ordering App: Common Mistakes in Interviews
Not handling empty cart or no results, allowing negative quantity, incorrect total calculation, spending too much time on CSS, scattered state, not debouncing search, and not testing. Fix each with the appropriate technique.
When decrementing, check if the quantity drops to 0 or below. If so, delete the item from the cart: if (cart[id] <= 0) delete cart[id]. This prevents negative quantities and keeps the cart clean.
Sum price * quantity for each item: Object.entries(cart).reduce((sum, [id, qty]) => { const item = menu.find(m => m.id === parseInt(id)); return sum + item.price * qty; }, 0). Remember to parseInt the ID and multiply price by quantity.
Still have questions?
Browse all our FAQs or reach out to our support team
