diff --git a/src/App.js b/src/App.js
index e134ca03..3570cae8 100644
--- a/src/App.js
+++ b/src/App.js
@@ -1,34 +1,43 @@
-import React, { useState } from 'react';
-import { Route } from 'react-router-dom';
-import data from './data';
+import React, { useState } from "react";
+import { Route } from "react-router-dom";
+import data from "./data";
// Components
-import Navigation from './components/Navigation';
-import Products from './components/Products';
-import ShoppingCart from './components/ShoppingCart';
+import Navigation from "./components/Navigation";
+import Products from "./components/Products";
+import ShoppingCart from "./components/ShoppingCart";
+
+// Context
+import { ProductContext } from "./contexts/ProductContext";
+import { CartContext } from "./contexts/CartContext";
function App() {
- const [products] = useState(data);
- const [cart, setCart] = useState([]);
-
- const addItem = item => {
- // add the given item to the cart
- };
-
- return (
-
-
-
- {/* Routes */}
-
-
-
-
-
-
-
-
- );
+ const [products] = useState(data);
+ const [cart, setCart] = useState([]);
+
+ const addItem = (item) => {
+ // add the given item to the cart
+ setCart([...cart, item]);
+ };
+
+ return (
+
+
+
+
+ {/* Routes */}
+
+
+
+
+
+
+
+
+
+
+
+ );
}
export default App;
diff --git a/src/components/Navigation.js b/src/components/Navigation.js
index b2ef658a..179866b6 100644
--- a/src/components/Navigation.js
+++ b/src/components/Navigation.js
@@ -1,15 +1,17 @@
-import React from 'react';
-import { NavLink } from 'react-router-dom';
+import React, { useContext } from "react";
+import { NavLink } from "react-router-dom";
+import { CartContext } from "../contexts/CartContext";
-const Navigation = props => {
- return (
-
- Products
-
- Cart {props.cart.length}
-
-
- );
+const Navigation = () => {
+ const cart = useContext(CartContext);
+ return (
+
+ Products
+
+ Cart {cart.length}
+
+
+ );
};
export default Navigation;
diff --git a/src/components/Product.js b/src/components/Product.js
index 8c9b860b..cd602c3d 100644
--- a/src/components/Product.js
+++ b/src/components/Product.js
@@ -1,19 +1,19 @@
-import React from 'react';
+import React, { useContext } from "react";
+import { ProductContext } from "../contexts/ProductContext";
-const Product = props => {
- return (
-
-

+const Product = (props) => {
+ const { addItem } = useContext(ProductContext);
+ return (
+
+

-
{props.product.title}
+
{props.product.title}
-
${props.product.price}
+
${props.product.price}
-
-
- );
+
+
+ );
};
export default Product;
diff --git a/src/components/Products.js b/src/components/Products.js
index 3f56b80e..2bd8b5ea 100644
--- a/src/components/Products.js
+++ b/src/components/Products.js
@@ -1,20 +1,18 @@
-import React from 'react';
+import React, { useContext } from "react";
// Components
-import Product from './Product';
+import Product from "./Product";
+import { ProductContext } from "../contexts/ProductContext";
-const Products = props => {
- return (
-
- {props.products.map(product => (
-
- ))}
-
- );
+const Products = (props) => {
+ const { products } = useContext(ProductContext);
+ return (
+
+ {products.map((product) => (
+
+ ))}
+
+ );
};
export default Products;
diff --git a/src/components/ShoppingCart.js b/src/components/ShoppingCart.js
index a2d21939..38e20333 100644
--- a/src/components/ShoppingCart.js
+++ b/src/components/ShoppingCart.js
@@ -1,27 +1,32 @@
-import React from 'react';
+import React, { useContext } from "react";
// Components
-import Item from './ShoppingCartItem';
+import Item from "./ShoppingCartItem";
+import { CartContext } from "../contexts/CartContext";
-const ShoppingCart = props => {
- const getCartTotal = () => {
- return props.cart.reduce((acc, value) => {
- return acc + value.price;
- }, 0).toFixed(2);
- };
+const ShoppingCart = () => {
+ const cart = useContext(CartContext);
+ console.log(cart);
+ const getCartTotal = () => {
+ return cart
+ .reduce((acc, value) => {
+ return acc + value.price;
+ }, 0)
+ .toFixed(2);
+ };
- return (
-
- {props.cart.map(item => (
-
- ))}
+ return (
+
+ {cart.map((item) => (
+
+ ))}
-
-
Total: ${getCartTotal()}
-
-
-
- );
+
+
Total: ${getCartTotal()}
+
+
+
+ );
};
export default ShoppingCart;
diff --git a/src/contexts/CartContext.js b/src/contexts/CartContext.js
new file mode 100644
index 00000000..22d69585
--- /dev/null
+++ b/src/contexts/CartContext.js
@@ -0,0 +1,3 @@
+import { createContext } from "react";
+
+export const CartContext = createContext();
diff --git a/src/contexts/ProductContext.js b/src/contexts/ProductContext.js
new file mode 100644
index 00000000..67ececc9
--- /dev/null
+++ b/src/contexts/ProductContext.js
@@ -0,0 +1,3 @@
+import { createContext } from "react";
+
+export const ProductContext = createContext();