Skip to content

Commit 48853c3

Browse files
committed
fix: resolve React hooks loadProducts hoisting issue in frontend
1 parent 16b964d commit 48853c3

3 files changed

Lines changed: 10 additions & 12 deletions

File tree

frontend/eslint.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default defineConfig([
1515
],
1616
languageOptions: {
1717
ecmaVersion: 2020,
18-
globals: globals.browser,
18+
globals: { ...globals.browser, ...globals.jest, ...globals.node },
1919
parserOptions: {
2020
ecmaVersion: 'latest',
2121
ecmaFeatures: { jsx: true },

frontend/src/App.jsx

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,17 @@ export default function App() {
1111
const [isCartOpen, setIsCartOpen] = useState(false);
1212

1313
useEffect(() => {
14+
async function loadProducts() {
15+
try {
16+
const data = await fetchProducts();
17+
setProducts(data);
18+
} catch {
19+
toast.error('Failed to load products');
20+
}
21+
}
1422
loadProducts();
1523
}, []);
1624

17-
const loadProducts = async () => {
18-
try {
19-
const data = await fetchProducts();
20-
setProducts(data);
21-
} catch (err) {
22-
toast.error('Failed to load products');
23-
}
24-
};
25-
2625
const handleAddToCart = (product) => {
2726
const existing = cartItems.find((item) => item.id === product.id);
2827
if (existing) {
@@ -50,7 +49,7 @@ export default function App() {
5049
toast.success(response.message || 'Checkout successful! 🎉', { duration: 4000 });
5150
setCartItems([]);
5251
setIsCartOpen(false);
53-
} catch (err) {
52+
} catch {
5453
toast.error('Checkout failed. Please try again.');
5554
}
5655
};

frontend/src/App.test.jsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { render, screen, waitFor } from '@testing-library/react';
2-
import userEvent from '@testing-library/user-event';
32
import App from './App';
43
import * as api from './api';
54

0 commit comments

Comments
 (0)