One suspense, two skeletons
Unanswered
PepeW posted this in #help-forum
PepeWOP
I have an async component making a fetch request. This component displays first one part of the response, then some "static" text and finally the last part of the response.
My component is wrapped in a
What I want to do is to display instead two skeletons, one for the first part and one for the last part while keeping the static text always visible.
I know that I could create a
Is there a better way to handle this situation ?
// My async component
export async function MyAsyncComponent() {
const response = await fetchRequest()
return (
<p>{response.first_part}</p>
<p>some static text</p>
<p>{response.last_part}</p>
)
}My component is wrapped in a
<Suspense /> so I can display a skeleton.<Suspense fallback={<Skeleton className="h-20 w-full" />}>
<MyAsyncComponent />
</Suspense>What I want to do is to display instead two skeletons, one for the first part and one for the last part while keeping the static text always visible.
I know that I could create a
<MyAsyncComponentSkeleton /> and use it as a fallback but I would duplicate some code.Is there a better way to handle this situation ?
3 Replies
Scottish Fold
I think the easiest way is simply create a loading.tsx file with your page layout, skeletons and the static. While the server is fetching, it will display your loading.tsx, then will load your page after fetched https://nextjs.org/docs/app/api-reference/file-conventions/loading
Cão Fila de São Miguel
Will that depends on your use case. If your page don't have any other data then you can go with loading.js approach.
If you have other data and you want to show loading on only these two you can go with suspense. And
<MyAsyncComponentSkeleton /> won't hurt