@@ -19,17 +19,22 @@ function App() {
19
19
{ id : 3 , name : 'Tomatoes' , quantity : 3 } ,
20
20
] )
21
21
22
- function addItem ( item : Item ) { }
22
+ function addItem ( item : Item ) {
23
+ // Add item to items array
24
+ }
23
25
24
26
// Without Cloning (Faster, More Difficult)
25
27
function subtractQuantity ( id : number ) {
26
- const index = items . findIndex ( ( item ) => item . id === id )
27
- const newItem = { ...items [ index ] , quantity : items [ index ] . quantity - 1 }
28
- if ( newItem . quantity === 0 ) {
29
- setItems ( [ ...items . slice ( 0 , index ) , ...items . slice ( index + 1 ) ] )
30
- } else {
31
- setItems ( [ ...items . slice ( 0 , index ) , newItem , ...items . slice ( index + 1 ) ] )
32
- }
28
+ // const index = items.findIndex((item) => item.id === id)
29
+ // const newItem = { ...items[index], quantity: items[index].quantity - 1 }
30
+ // OLD WAY
31
+ // if (newItem.quantity === 0) {
32
+ // setItems([...items.slice(0, index), ...items.slice(index + 1)])
33
+ // } else {
34
+ // setItems([...items.slice(0, index), newItem, ...items.slice(index + 1)])
35
+ // }
36
+ // NEW WAY
37
+ // setItems(items.with(index, newItem))
33
38
}
34
39
35
40
// With Cloning (Slower, Easier)
0 commit comments