React › Server Components
What is a React Server Component and how is it different from a regular component?
IntermediateAnswer
A Server Component renders entirely on the server, sending only the finished result to the browser instead of the actual JavaScript code needed to run it. This means a Server Component cannot use hooks like useState or respond to clicks directly, but it can access server only resources, like a database, directly and safely.
// A Server Component, this is the default in frameworks like Next.js
async function ProductList() {
const products = await db.products.findAll(); // safe, runs only on the server
return (
<ul>
{products.map((p) => <li key={p.id}>{p.name}</li>)}
</ul>
);
}Real-world example An online store fetches its product list directly from the database inside a Server Component, without needing to build a separate API endpoint or send any database logic to the visitor's browser at all.