#display a spinner example for me in nextjs 13
1 messages · Page 1 of 1 (latest)
Hi @vestal wing. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
In Next.js 13, you can display a loading spinner by using the loading.js file convention or the Suspense component from React.
Here's an example of how you can create a loading state using a loading.js file:
export default function Loading() {
return <p>Loading...</p>;
}
This file lives at the same level as the layout file that applies to the route, and its contents will be displayed until all data fetching in the route segment has finished. You can replace the <p>Loading...</p> with your spinner component.
Alternatively, you can use the Suspense component to display a loading spinner for specific parts of your UI. Here's an example:
import { Suspense } from 'react'
import { PostFeed, Weather } from './Components'
export default function Posts() {
return (
<section>
<Suspense fallback={<p>Loading feed...</p>}>
<PostFeed />
</Suspense>
<Suspense fallback={<p>Loading weather...</p>}>
<Weather />
</Suspense>
</section>
)
}
In this example, the <p>Loading feed...</p> and <p>Loading weather...</p> would be replaced with your spinner component.
Please note that these examples do not include the actual spinner component. You would need to create or import a spinner component and replace the placeholder text with it.