add cart context and provider

This commit is contained in:
oscar 2023-03-18 13:06:20 +13:00
parent 4130321108
commit 42813be6a6
3 changed files with 34 additions and 2 deletions

View File

@ -3,6 +3,7 @@ import React, { useState } from "react";
import Header from "./components/Layout/Header"; import Header from "./components/Layout/Header";
import Meals from "./components/Meals/Meals"; import Meals from "./components/Meals/Meals";
import Cart from "./components/Cart/Cart"; import Cart from "./components/Cart/Cart";
import CartProvider from "./store/CartProvider";
const App = () => { const App = () => {
const [cartIsShown, setCartIsShown] = useState(false); const [cartIsShown, setCartIsShown] = useState(false);
@ -16,7 +17,7 @@ const App = () => {
}; };
return ( return (
<> <CartProvider>
{cartIsShown && ( {cartIsShown && (
<Cart onShowCart={showCartHandler} onHideCart={hideCartHandler} /> <Cart onShowCart={showCartHandler} onHideCart={hideCartHandler} />
)} )}
@ -24,7 +25,7 @@ const App = () => {
<main> <main>
<Meals /> <Meals />
</main> </main>
</> </CartProvider>
); );
}; };

21
src/store/CartProvider.js Normal file
View File

@ -0,0 +1,21 @@
import CartContext from "./cart-context";
const CartProvider = (props) => {
const addItemToCartHandler = (item) => {};
const removeItemFromCartHandler = (id) => {};
const cartContext = {
items: [],
totalAmount: 0,
addItem: addItemToCartHandler,
removeItem: removeItemFromCartHandler,
};
return (
<CartContext.Provider value={cartContext}>
{props.children}
</CartContext.Provider>
);
};
export default CartProvider;

10
src/store/cart-context.js Normal file
View File

@ -0,0 +1,10 @@
import React from "react";
const CartContext = React.createContext({
items: [],
totalAmount: 0,
addItem: (item) => {},
removeItem: (id) => {},
});
export default CartContext;