ReactJS Program to Cart Item Quantity Controls with Explanation
ReactJS
Medium
State Management
27 views
1 min read
87 words
This problem helps you practice core ReactJS fundamentals in a practical way. It builds intuition around item, cart, quantity. Let’s break it down step by step so you can implement it confidently.
Problem Statement
Build a cart where you can increase/decrease quantity per item.
Input Format
No input.
Output Format
Render a React component.
Constraints
Update items immutably using map and filter.
Code Solution
This explanation is written for learning purposes and to help beginners understand the concept clearly.
import React, { useMemo, useState } from 'react';
const initial = [
{ id: 'r1', name: 'React Basics', qty: 1 },
{ id: 'r2', name: 'Hooks', qty: 2 }
];
export default function App() {
const [items, setItems] = useState(initial);
function change(id, delta) {
setItems((prev) => {
const next = prev.map((x) => (x.id === id ? { ...x, qty: x.qty + delta } : x));
return next.filter((x) => x.qty > 0);
});
}
const total = useMemo(() => items.reduce((s, x) => s + x.qty, 0), [items]);
return (
<div style={{ padding: 16, width: 520 }}>
<h2 style={{ marginTop: 0 }}>meetcode cart</h2>
<div style={{ color: '#555' }}>Total items: {total}</div>
<ul style={{ marginTop: 12, paddingLeft: 18 }}>
{items.map((it) => (
<li key={it.id} style={{ marginBottom: 10 }}>
<strong>{it.name}</strong> — {it.qty}
<div style={{ display: 'flex', gap: 8, marginTop: 6 }}>
<button type='button' onClick={() => change(it.id, -1)} style={{ padding: '6px 10px', borderRadius: 12, border: '1px solid #ddd', background: '#fff' }}>-</button>
<button type='button' onClick={() => change(it.id, +1)} style={{ padding: '6px 10px', borderRadius: 12, border: 0, background: '#0b5', color: '#fff' }}>+</button>
</div>
</li>
))}
</ul>
</div>
);
}
Output Example
No sample I/O is provided for this question.
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Solution Guide
Problem
Build a cart where you can increase/decrease quantity per item.
Input / Output
Output
Render a React component.
Constraints
Update items immutably using map and filter.
Details
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Difficulty
Medium
ReactJS
Official Solution
import React, { useMemo, useState } from 'react';
const initial = [
{ id: 'r1', name: 'React Basics', qty: 1 },
{ id: 'r2', name: 'Hooks', qty: 2 }
];
export default function App() {
const [items, setItems] = useState(initial);
function change(id, delta) {
setItems((prev) => {
const next = prev.map((x) => (x.id === id ? { ...x, qty: x.qty + delta } : x));
return next.filter((x) => x.qty > 0);
});
}
const total = useMemo(() => items.reduce((s, x) => s + x.qty, 0), [items]);
return (
<div style={{ padding: 16, width: 520 }}>
<h2 style={{ marginTop: 0 }}>meetcode cart</h2>
<div style={{ color: '#555' }}>Total items: {total}</div>
<ul style={{ marginTop: 12, paddingLeft: 18 }}>
{items.map((it) => (
<li key={it.id} style={{ marginBottom: 10 }}>
<strong>{it.name}</strong> — {it.qty}
<div style={{ display: 'flex', gap: 8, marginTop: 6 }}>
<button type='button' onClick={() => change(it.id, -1)} style={{ padding: '6px 10px', borderRadius: 12, border: '1px solid #ddd', background: '#fff' }}>-</button>
<button type='button' onClick={() => change(it.id, +1)} style={{ padding: '6px 10px', borderRadius: 12, border: 0, background: '#0b5', color: '#fff' }}>+</button>
</div>
</li>
))}
</ul>
</div>
);
}
Solutions (0)
No solutions submitted yet. Be the first!