diff --git a/src/App.js b/src/App.js
index e134ca03..25e16875 100644
--- a/src/App.js
+++ b/src/App.js
@@ -1,6 +1,8 @@
import React, { useState } from 'react';
import { Route } from 'react-router-dom';
import data from './data';
+import ProductContext from './contexts/ProductContext';
+import CartContext from './contexts/CartContext';
// Components
import Navigation from './components/Navigation';
@@ -12,21 +14,24 @@ function App() {
const [cart, setCart] = useState([]);
const addItem = item => {
- // add the given item to the cart
+ setCart([...cart, item]);
};
return (
-
+
+
{/* Routes */}
+
-
+
-
+
-
+
+
);
}
diff --git a/src/components/Navigation.js b/src/components/Navigation.js
index b2ef658a..f7162414 100644
--- a/src/components/Navigation.js
+++ b/src/components/Navigation.js
@@ -1,12 +1,14 @@
-import React from 'react';
+import React, { useContext } from 'react';
import { NavLink } from 'react-router-dom';
+import CartContext from './../contexts/CartContext';
const Navigation = props => {
+ const cart = useContext(CartContext);
return (
Products
- Cart {props.cart.length}
+ Cart {cart.length}
);
diff --git a/src/components/Product.js b/src/components/Product.js
index 8c9b860b..29461bac 100644
--- a/src/components/Product.js
+++ b/src/components/Product.js
@@ -1,6 +1,8 @@
-import React from 'react';
+import React, { useContext } from 'react';
+import ProductContext from './../contexts/ProductContext';
const Product = props => {
+ const { addItem } = useContext(ProductContext);
return (

@@ -9,7 +11,7 @@ const Product = props => {
${props.product.price}
-
diff --git a/src/components/Products.js b/src/components/Products.js
index 3f56b80e..ef816c42 100644
--- a/src/components/Products.js
+++ b/src/components/Products.js
@@ -1,16 +1,17 @@
-import React from 'react';
+import React, { useContext } from 'react';
+import ProductContext from './../contexts/ProductContext';
// Components
import Product from './Product';
const Products = props => {
+ const { products } = useContext(ProductContext);
return (
- {props.products.map(product => (
+ {products.map(product => (
))}
diff --git a/src/components/ShoppingCart.js b/src/components/ShoppingCart.js
index a2d21939..9f306730 100644
--- a/src/components/ShoppingCart.js
+++ b/src/components/ShoppingCart.js
@@ -1,18 +1,20 @@
-import React from 'react';
+import React, { useContext } from 'react';
+import CartContext from './../contexts/CartContext';
// Components
import Item from './ShoppingCartItem';
const ShoppingCart = props => {
+ const cart = useContext(CartContext);
const getCartTotal = () => {
- return props.cart.reduce((acc, value) => {
+ return cart.reduce((acc, value) => {
return acc + value.price;
}, 0).toFixed(2);
};
return (
- {props.cart.map(item => (
+ {cart.map(item => (
))}
diff --git a/src/contexts/CartContext.js b/src/contexts/CartContext.js
new file mode 100644
index 00000000..62adec9a
--- /dev/null
+++ b/src/contexts/CartContext.js
@@ -0,0 +1,5 @@
+import { createContext } from 'react';
+
+const CartContext = createContext();
+
+export default CartContext;
\ No newline at end of file
diff --git a/src/contexts/ProductContext.js b/src/contexts/ProductContext.js
new file mode 100644
index 00000000..57c23c12
--- /dev/null
+++ b/src/contexts/ProductContext.js
@@ -0,0 +1,5 @@
+import { createContext } from 'react';
+
+const ProductContext = createContext();
+
+export default ProductContext;
\ No newline at end of file