I'll create an example that demonstrates how to pass data from the server to fill a paragraph in a Next.js application using Server Components. This example will show you how to fetch data on the server and render it in a client-side component.
In this example:
page.tsx
file is a server component. It uses the getData
function to fetch data from an API. This function is asynchronous and uses the fetch
API, which is available in Next.js server components 1.Paragraph
component as a prop named content
.Paragraph
component is a client component (note the 'use client'
directive at the top). It receives the content
prop and displays it in a paragraph.Paragraph
component includes a "Read more/Read less" toggle. This demonstrates how you can combine server-side data fetching with client-side interactivity.line-clamp-3
Tailwind class to limit the text to three lines when not expanded 2.This example showcases how you can:
Remember that the server component (page.tsx
) runs on the server, so the API call happens there. The resulting data is then passed to the client component (Paragraph.tsx
) which runs in the browser. This approach helps with performance and SEO, as the initial data is fetched server-side 1.
Sources