Build a bounded quantity picker
Keep a quantity inside its limits
A bounded quantity picker permits only a defined interval. Here the local packing request accepts 0 to 6 whole boxes. Disable controls at the boundaries for usability, and also enforce the bounds inside the updater so queued changes cannot cross the limits. This is a local UI rule, not authoritative inventory validation.
import { useState } from 'react';
function adjustQuantity(quantity, step) { return Math.max(0, Math.min(6, quantity + step));}
export default function App() { const [quantity, setQuantity] = useState(2); return <main><h1>Request boxes</h1><p role="status">Quantity: {quantity}</p> <button type="button" disabled={quantity === 0} onClick={() => setQuantity(q => adjustQuantity(q, -1))}>Remove one</button> <button type="button" disabled={quantity === 6} onClick={() => setQuantity(q => adjustQuantity(q, 1))}>Add one</button> <button type="button" onClick={() => setQuantity(2)}>Reset</button> </main>;}Start at 2; remove twice to reach zero, where Remove one is disabled. Add repeatedly to reach six; Add one is disabled. Reset restores two. Math.min and Math.max cap the result; adjustQuantity assumes integer state and integer step. It is not a general input sanitizer. A functional updater keeps the calculation based on the pending quantity.
Add a batch without exceeding capacity
import { useState } from 'react';
export default function App() { const [quantity, setQuantity] = useState(5); return <main><h1>Request boxes in batches</h1><p role="status">Quantity: {quantity}</p> <button type="button" disabled={quantity === 6} onClick={() => setQuantity(q => Math.min(6, q + 3))}>Add up to three</button> <button type="button" onClick={() => setQuantity(5)}>Reset</button> </main>;}At five, one activation produces six rather than eight. This example intentionally adds only; its initial and reset values are within the valid interval. The button label describes the possibility of a partial batch. The server would still need to enforce real stock limits if the request left the browser.
Calculate initial state only when needed
import { useState } from 'react';
function initialQuantity() { const planned = [1, 2]; return planned.reduce((total, boxes) => total + boxes, 0);}
export default function App() { const [quantity, setQuantity] = useState(initialQuantity); return <main><h1>Packing plan</h1><p role="status">Quantity: {quantity}</p> <button type="button" disabled={quantity === 6} onClick={() => setQuantity(q => Math.min(6, q + 1))}>Add one</button> <button type="button" onClick={() => setQuantity(initialQuantity())}>Reset to plan</button> </main>;}The fixed plan totals three. Passing the initializer itself lets React compute the starting state on initialization rather than evaluating initialQuantity() on each component call. A reset is explicit: this handler calls the helper and requests three. The plan is intentionally tiny to demonstrate syntax, not to claim a performance gain. In development StrictMode the pure initializer may be invoked twice and one result ignored. It must not consume inventory or create external records.
Practice
Adapt the first picker to a limit of 8 and initial quantity 3. Add a button that increases the value by two without exceeding 8. Check both ends, several successive activations, reset and keyboard access. Explain why the cap belongs inside the updater.