State

Replace an array state item

Map the array and replace only the record selected by its stable ID.

Formal Syntax & Structure

react syntaxreact
setItems(old => old.map(item => item.id === id ? { ...item, quantity } : item))

Working Example

Example Snippetreact
setItems(old => old.map(i => i.id === "mug" ? { ...i, quantity: 0 } : i))
React Reference