Created
February 7, 2023 18:08
-
-
Save AndrewIngram/d04ed29937f6c175b8fdd93ce1518950 to your computer and use it in GitHub Desktop.
Remix-inspired "Await" component for RSC / Next 13
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
import { use } from "react"; | |
type Props<T> = { | |
resolve: Promise<T>; | |
children: (value: T) => React.ReactElement; | |
}; | |
export default function Await<T>({ resolve, children }: Props<T>) { | |
const value = use(resolve); | |
return children(value); | |
} |
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
import { Suspense } from "react"; | |
import Await from "~/components/Await"; | |
import { | |
ProductView, | |
ReviewsView, | |
getProductReviews, | |
getProduct, | |
Skelly, | |
} from "somewhere-I-guess"; | |
type Props = { | |
id: string; | |
}; | |
export default async function Product({ id }: Props) { | |
const productReviewsPromise = getProductReviews(id); | |
const product = await getProduct(id); | |
return ( | |
<> | |
<ProductView data={product} /> | |
<Suspense fallback={<Skelly />}> | |
<Await resolve={productReviewsPromise}> | |
{(reviews) => <ReviewsView data={reviews} />} | |
</Await> | |
</Suspense> | |
</> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment