#Using async in server components.

1 messages · Page 1 of 1 (latest)

humble socketBOT
#

Hi @little gazelle. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Yes, you can use async in your page.tsx component in the app directory. This is a feature of Next.js's Server Components. Here's an example from the Next.js documentation (https://nextjs.org/docs/app/building-your-application/upgrading/app-router-migration#step-4-migrating-pages):

async function getPosts() {
 const res = await fetch('<https://...'>)
 const posts = await res.json()
 return posts
}

export default async function Page() {
 // Fetch data directly in a Server Component
 const recentPosts = await getPosts()
 // Forward fetched data to your Client Component
 return <HomePage recentPosts={recentPosts} />
}

In your case, you can fetch your approvedEvents and currentUser data directly in the Server Component:

async function getEvents() {
 // Your logic to fetch events
}

async function getCurrentUser() {
 // Your logic to fetch current user
}

export default async function Home() {
 const approvedEvents = await getEvents();
 const currentUser = await getCurrentUser();
 // Your logic to use the fetched data
}

Remember to handle any potential errors that might occur during the data fetching.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: