Files
59f443a7-116a-4bc5-bb20-5fd…/src/hooks/useProduct.ts
2026-03-08 22:21:18 +00:00

39 lines
904 B
TypeScript

import { useState, useEffect } from 'react';
interface Product {
id: string;
name: string;
price: string;
imageSrc: string;
}
export function useProduct(productId: string) {
const [product, setProduct] = useState<Product | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
const fetchProduct = async () => {
try {
setLoading(true);
// Simulate fetch
const response = await fetch(`/api/products/${productId}`);
const data = await response.json();
setProduct(data);
setError(null);
} catch (err) {
setError('Failed to fetch product');
setProduct(null);
} finally {
setLoading(false);
}
};
if (productId) {
fetchProduct();
}
}, [productId]);
return { product, loading, error };
}