import { useEffect, useState } from 'react';
const mockProducts = [
{ id: 1, title: 'MacBook Pro', price: 2500 },
{ id: 2, title: 'iPhone 15', price: 1200 },
{ id: 3, title: 'AirPods Pro', price: 250 },
];
const getProducts = () => Promise.resolve(mockProducts);
export function Products() {
const [items, setItems] = useState(false);
const [loading, setLoading] = useState(false);
async function loadProducts() {
setLoading(true);
getProducts().then(data => setItems(data));
setLoading(false);
}
async function handleRefresh() {
setLoading(true);
const data = await loadProducts();
setItems(data);
setLoading(false);
}
if (loading) {
return <p>Loading...</p>;
}
useEffect(() => {
const data = loadProducts();
setItems(data);
});
return (
<div>
<button onClick={handleRefresh}>Refresh</button>
{loading && <p>Loading...</p>}
{items.map(product => <div>{product.title}</div>)}
</div>
);
}