Product List with Cart

Please log in to post a comment
Log in with GitHubCommunity feedback
- P@olaide-hok
Great work here!
🔧 Here are some improvement recommendations
1. Performance Optimizations
- Inline functions in render may cause unnecessary re-renders
- No memoization of expensive calculations
You can consider the approach below:
// Memoize product list const products = useMemo(() => data.map(item => ( <ProductCard key={item.id} item={item} {...props} /> )), [data, props]); // Memoize total calculations const totalQuantity = useMemo(() => ( cartItems.reduce((sum, item) => sum + item.quantity, 0) ), [cartItems]);
2. Cart Logic Enhancements
- Duplicate items can be added to cart
- Quantity updates could be more efficient
You can consider the approach below:
function addToCart(productItem) { setCartItems(current => { const existing = current.find(item => item.id === productItem.id); return existing ? current.map(item => item.id === productItem.id ? {...item, quantity: item.quantity + 1} : item ) : [...current, {...productItem, quantity: 1}]; }); }
3. Component Improvements
A. Extract
CartItem
andProductCard
- Both
Cart
andProductList
contain large repetitive JSX blocks.
Suggestion:
- Extract
ProductCard
for each product inProductList
- Extract
CartItem
for each item inCart
- Extract
OrderItem
from Order for consistency
This would improve readability, testability, and component reusability.
Overall, your code was well structured, the UX was responsive and good state management with the
useState
hook.Happy Coding!
Marked as helpful
Join our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord