generated from LankyMoose/pnpm-monorepo-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
75 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { useAsync, useState } from "kaioken" | ||
|
||
interface Product { | ||
id: number | ||
title: string | ||
description: string | ||
price: number | ||
discountPercentage: number | ||
rating: number | ||
stock: number | ||
brand: string | ||
category: string | ||
thumbnail: string | ||
images: string[] | ||
} | ||
|
||
export function UseAsyncExample() { | ||
const [productId, setProductId] = useState(1) | ||
|
||
const data = useAsync(async () => { | ||
const res = await fetch(`https://dummyjson.com/products/${productId}`) | ||
await new Promise((res) => setTimeout(res, Math.random() * 3000)) | ||
return res.json() as Promise<Product> | ||
}, [productId]) | ||
|
||
console.log("data", data) | ||
|
||
return ( | ||
<div> | ||
<button onclick={() => setProductId((prev) => prev + 1)}>Next</button> | ||
{data ? <ProductCard product={data} /> : <p>Loading...</p>} | ||
</div> | ||
) | ||
} | ||
|
||
function ProductCard({ product }: { product: Product }) { | ||
return ( | ||
<div> | ||
<h1> | ||
{product.title} <sup>({product.id})</sup> | ||
</h1> | ||
<p>{product.description}</p> | ||
|
||
<img src={product.thumbnail} /> | ||
</div> | ||
) | ||
} |