Skip to content

Commit 88a1cb3

Browse files
Merge pull request #1 from Chandan9898Kumar/NewRepo
Feat: Added new repo which depicts update and remove of data.
2 parents 6cf7d38 + 0766011 commit 88a1cb3

File tree

3 files changed

+112
-21
lines changed

3 files changed

+112
-21
lines changed

src/App.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@ import './App.css';
22
import { BrowserRouter, Route, Routes } from "react-router-dom";
33
import Homes from './Home';
44
import ObjectForm from './Components/ObjectsChanges';
5-
import StateList from './Components/StateChanges'
5+
import StateList from './Components/StateChanges';
6+
import ShoppingCart from './Components/UpdateCart'
67
function App() {
78
return (
89
<div className="App">
9-
<BrowserRouter>
10-
<Routes>
11-
<Route exact path='/' element={<Homes />} />
12-
<Route exact path='/ObjectChange' element={<ObjectForm />} />
10+
<BrowserRouter>
11+
<Routes>
12+
<Route exact path='/' element={<Homes />} />
13+
<Route exact path='/ObjectChange' element={<ObjectForm />} />
14+
<Route exact path='/StateChange' element={<StateList />} />
15+
<Route exact path='/ShpingCart' element={<ShoppingCart />} />
16+
</Routes>
17+
</BrowserRouter>
1318

14-
<Route exact path='/StateChange' element={<StateList />} />
15-
16-
17-
</Routes>
18-
</BrowserRouter>
19-
2019
</div>
2120
);
2221
}

src/Components/UpdateCart.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { useState } from 'react';
2+
3+
const initialProducts = [{
4+
id: 0,
5+
name: 'Baklava',
6+
count: 1,
7+
}, {
8+
id: 1,
9+
name: 'Cheese',
10+
count: 5,
11+
}, {
12+
id: 2,
13+
name: 'Spaghetti',
14+
count: 2,
15+
}];
16+
17+
const ShoppingCart = () => {
18+
const [products, setProducts] = useState(initialProducts)
19+
20+
21+
// Update an item in the shopping cart
22+
function handleIncreaseClick(productId) {
23+
setProducts(products.map((item) =>{
24+
if(item.id===productId){
25+
return {...item,count:item.count+1}
26+
}else{
27+
return item
28+
}
29+
}))
30+
31+
// OR We can do it like below as well .
32+
33+
// const data = products.map((item) => {
34+
// if (item.id === productId) {
35+
// return { ...item, count: item.count + 1 }
36+
// } else {
37+
// return item
38+
// }
39+
// })
40+
// setProducts(data)
41+
42+
}
43+
44+
//
45+
const handleDecreaseClick =(ProductId)=>{
46+
const data=products.map((item)=>{
47+
if(item.id===ProductId){
48+
return {...item,count:item.count-1}
49+
}else{
50+
return item
51+
}
52+
})
53+
// if your count is 0 then that item should be removed, uncomment below and check.
54+
55+
// const result=data.filter((item)=> item.count>0)
56+
// setProducts(result)
57+
setProducts(data)
58+
59+
60+
61+
// setProducts(products.map((item) =>{
62+
// if(item.id===productId){
63+
// return {...item,count:item.count+1}
64+
// }else{
65+
// return item
66+
// }
67+
68+
// }))
69+
}
70+
71+
return (
72+
<ul>
73+
{products.map(product => (
74+
<li key={product.id}>
75+
{product.name}
76+
{' '}
77+
(<b>{product.count}</b>)
78+
<button onClick={() => {
79+
handleIncreaseClick(product.id);
80+
}}>
81+
+
82+
</button>
83+
<button onClick={() => {
84+
handleDecreaseClick(product.id);
85+
}}>
86+
-
87+
</button>
88+
</li>
89+
))}
90+
</ul>
91+
);
92+
}
93+
export default ShoppingCart;

src/Home.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import React from 'react'
22
import { NavLink } from "react-router-dom"
3-
const Homes=()=>{
4-
return(
5-
<>
6-
<label>Home page</label><br /><br />
7-
<NavLink to='/ObjectChange'>Change Nested Object</NavLink><br /><br />
8-
<NavLink to='/StateChange'>Change Nested State</NavLink><br /><br />
9-
10-
11-
</>
12-
)
3+
const Homes = () => {
4+
return (
5+
<>
6+
<label>Home page</label><br /><br />
7+
<NavLink to='/ObjectChange'>Change Nested Object</NavLink><br /><br />
8+
<NavLink to='/StateChange'>Change Nested State</NavLink><br /><br />
9+
<NavLink to='/ShpingCart'>Change Nested State of Cart</NavLink><br /><br />
10+
</>
11+
)
1312
}
1413
export default Homes

0 commit comments

Comments
 (0)