Skip to content

Instantly share code, notes, and snippets.

@AndrewIngram
Created February 7, 2023 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndrewIngram/d04ed29937f6c175b8fdd93ce1518950 to your computer and use it in GitHub Desktop.
Save AndrewIngram/d04ed29937f6c175b8fdd93ce1518950 to your computer and use it in GitHub Desktop.
Remix-inspired "Await" component for RSC / Next 13
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);
}
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