-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Chandan9898Kumar/NewRepo
Feat: Added new repo which depicts update and remove of data.
- Loading branch information
Showing
3 changed files
with
112 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { useState } from 'react'; | ||
|
||
const initialProducts = [{ | ||
id: 0, | ||
name: 'Baklava', | ||
count: 1, | ||
}, { | ||
id: 1, | ||
name: 'Cheese', | ||
count: 5, | ||
}, { | ||
id: 2, | ||
name: 'Spaghetti', | ||
count: 2, | ||
}]; | ||
|
||
const ShoppingCart = () => { | ||
const [products, setProducts] = useState(initialProducts) | ||
|
||
|
||
// Update an item in the shopping cart | ||
function handleIncreaseClick(productId) { | ||
setProducts(products.map((item) =>{ | ||
if(item.id===productId){ | ||
return {...item,count:item.count+1} | ||
}else{ | ||
return item | ||
} | ||
})) | ||
|
||
// OR We can do it like below as well . | ||
|
||
// const data = products.map((item) => { | ||
// if (item.id === productId) { | ||
// return { ...item, count: item.count + 1 } | ||
// } else { | ||
// return item | ||
// } | ||
// }) | ||
// setProducts(data) | ||
|
||
} | ||
|
||
// | ||
const handleDecreaseClick =(ProductId)=>{ | ||
const data=products.map((item)=>{ | ||
if(item.id===ProductId){ | ||
return {...item,count:item.count-1} | ||
}else{ | ||
return item | ||
} | ||
}) | ||
// if your count is 0 then that item should be removed, uncomment below and check. | ||
|
||
// const result=data.filter((item)=> item.count>0) | ||
// setProducts(result) | ||
setProducts(data) | ||
|
||
|
||
|
||
// setProducts(products.map((item) =>{ | ||
// if(item.id===productId){ | ||
// return {...item,count:item.count+1} | ||
// }else{ | ||
// return item | ||
// } | ||
|
||
// })) | ||
} | ||
|
||
return ( | ||
<ul> | ||
{products.map(product => ( | ||
<li key={product.id}> | ||
{product.name} | ||
{' '} | ||
(<b>{product.count}</b>) | ||
<button onClick={() => { | ||
handleIncreaseClick(product.id); | ||
}}> | ||
+ | ||
</button> | ||
<button onClick={() => { | ||
handleDecreaseClick(product.id); | ||
}}> | ||
- | ||
</button> | ||
</li> | ||
))} | ||
</ul> | ||
); | ||
} | ||
export default ShoppingCart; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,13 @@ | ||
import React from 'react' | ||
import { NavLink } from "react-router-dom" | ||
const Homes=()=>{ | ||
return( | ||
<> | ||
<label>Home page</label><br /><br /> | ||
<NavLink to='/ObjectChange'>Change Nested Object</NavLink><br /><br /> | ||
<NavLink to='/StateChange'>Change Nested State</NavLink><br /><br /> | ||
|
||
|
||
</> | ||
) | ||
const Homes = () => { | ||
return ( | ||
<> | ||
<label>Home page</label><br /><br /> | ||
<NavLink to='/ObjectChange'>Change Nested Object</NavLink><br /><br /> | ||
<NavLink to='/StateChange'>Change Nested State</NavLink><br /><br /> | ||
<NavLink to='/ShpingCart'>Change Nested State of Cart</NavLink><br /><br /> | ||
</> | ||
) | ||
} | ||
export default Homes |