add cart context and provider
This commit is contained in:
parent
4130321108
commit
42813be6a6
@ -3,6 +3,7 @@ import React, { useState } from "react";
|
||||
import Header from "./components/Layout/Header";
|
||||
import Meals from "./components/Meals/Meals";
|
||||
import Cart from "./components/Cart/Cart";
|
||||
import CartProvider from "./store/CartProvider";
|
||||
|
||||
const App = () => {
|
||||
const [cartIsShown, setCartIsShown] = useState(false);
|
||||
@ -16,7 +17,7 @@ const App = () => {
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<CartProvider>
|
||||
{cartIsShown && (
|
||||
<Cart onShowCart={showCartHandler} onHideCart={hideCartHandler} />
|
||||
)}
|
||||
@ -24,7 +25,7 @@ const App = () => {
|
||||
<main>
|
||||
<Meals />
|
||||
</main>
|
||||
</>
|
||||
</CartProvider>
|
||||
);
|
||||
};
|
||||
|
||||
|
21
src/store/CartProvider.js
Normal file
21
src/store/CartProvider.js
Normal 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
10
src/store/cart-context.js
Normal file
@ -0,0 +1,10 @@
|
||||
import React from "react";
|
||||
|
||||
const CartContext = React.createContext({
|
||||
items: [],
|
||||
totalAmount: 0,
|
||||
addItem: (item) => {},
|
||||
removeItem: (id) => {},
|
||||
});
|
||||
|
||||
export default CartContext;
|
Loading…
Reference in New Issue
Block a user