Skip to content

Commit 0bbfe7d

Browse files
refactor: expose useShoppingCart
1 parent 8744280 commit 0bbfe7d

File tree

5 files changed

+20
-14
lines changed

5 files changed

+20
-14
lines changed

code/10 Advanced State Management with Context useReducer/09-dispatching-actions-finished/src/App.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Header from './components/Header.jsx';
22
import Shop from './components/Shop.jsx';
33
import Product from './components/Product.jsx';
44
import { DUMMY_PRODUCTS } from './dummy-products.js';
5-
import ShoppingCartProvider from './store/shopping-cart-context.jsx';
5+
import { ShoppingCartProvider } from './store/shopping-cart-context.jsx';
66

77
function App() {
88
return (

code/10 Advanced State Management with Context useReducer/09-dispatching-actions-finished/src/components/Cart.jsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import { useContext } from 'react';
2-
3-
import { ShoppingCartContext } from '../store/shopping-cart-context.jsx';
1+
import { useShoppingCart } from '../store/shopping-cart-context.jsx';
42

53
export default function Cart() {
6-
const { state: { items }, dispatch } = useContext(ShoppingCartContext);
4+
const { state: { items }, dispatch } = useShoppingCart();
75

86
const totalPrice = items.reduce(
97
(acc, item) => acc + item.price * item.quantity,

code/10 Advanced State Management with Context useReducer/09-dispatching-actions-finished/src/components/Header.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { useRef, useContext } from 'react';
1+
import { useRef } from 'react';
22

33
import CartModal from './CartModal.jsx';
4-
import { ShoppingCartContext } from '../store/shopping-cart-context.jsx';
4+
import { useShoppingCart } from '../store/shopping-cart-context.jsx';
55

66
export default function Header() {
77
const modal = useRef();
8-
const { state: { items } } = useContext(ShoppingCartContext);
8+
const { state: { items } } = useShoppingCart();
99

1010
const cartQuantity = items.length;
1111

code/10 Advanced State Management with Context useReducer/09-dispatching-actions-finished/src/components/Product.jsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import { useContext } from 'react';
2-
3-
import { ShoppingCartContext } from '../store/shopping-cart-context.jsx';
1+
import { useShoppingCart } from '../store/shopping-cart-context.jsx';
42

53
export default function Product({ id, image, title, price, description }) {
6-
const { shoppingCartDispatch } = useContext(ShoppingCartContext);
4+
const { shoppingCartDispatch } = useShoppingCart();
75

86
return (
97
<article className="product">

code/10 Advanced State Management with Context useReducer/09-dispatching-actions-finished/src/store/shopping-cart-context.jsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createContext, useReducer } from 'react';
1+
import { createContext, useReducer, useContext } from 'react';
22

33
import { DUMMY_PRODUCTS } from '../dummy-products.js';
44

@@ -67,7 +67,7 @@ function shoppingCartReducer(state, action) {
6767
}
6868
}
6969

70-
export default function ShoppingCartProvider({ children }) {
70+
function ShoppingCartProvider({ children }) {
7171
const [state, dispatch] = useReducer(shoppingCartReducer, { items: [] }
7272
);
7373

@@ -80,3 +80,13 @@ export default function ShoppingCartProvider({ children }) {
8080
<ShoppingCartContext.Provider value={value}>{children}</ShoppingCartContext.Provider>
8181
);
8282
}
83+
84+
function useShoppingCart() {
85+
const context = useContext(ShoppingCartContext);
86+
if (!context) {
87+
throw new Error('useShoppingCart must be used within a ShoppingCartProvider');
88+
}
89+
return context;
90+
}
91+
92+
export { ShoppingCartProvider, useShoppingCart };

0 commit comments

Comments
 (0)