File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
recipes/javascript/packages/react Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 1+ # Shopping cart
2+
3+
4+ ``` jsx
5+ import { useState } from ' react' ;
6+
7+ function ShoppingCart () {
8+ const [cart , setCart ] = useState ([]);
9+
10+ function addItemToCart (e ) {
11+ const item = e .target .value ;
12+ console .log (item);
13+ setCart ([... cart, item]);
14+ }
15+
16+ return (
17+ < div className= " app" >
18+ < div className= " items" >
19+ < button value= " MacBook Pro" onClick= {addItemToCart}> MacBook Pro< / button>
20+ < button value= " iPhone XS" onClick= {addItemToCart}> iPhone XS < / button>
21+ < button value= " Gem" onClick= {addItemToCart}> Gem< / button>
22+ < button value= " Teddy Bear" onClick= {addItemToCart}> Teddy Bear< / button>
23+ < / div>
24+
25+ < div className= " cart" >
26+ Cart
27+ < ul>
28+ {
29+ cart .map (item =>
30+ < li key= {item}> {item}< / li>
31+ )
32+ }
33+ < / ul>
34+ < / div>
35+ < / div>
36+ );
37+ }
38+
39+ export default ShoppingCart ;
40+ ```
You can’t perform that action at this time.
0 commit comments