Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
React Server Components are shaking up the front-end world like a caffeinated squirrel in a nut factory. But what’s all the fuss about? Let’s dive in and see why they’re the hottest thing since sliced JSX.
Server Components are a new way to build React applications that leverage the power of the server. They allow you to render components on the server, reducing the amount of JavaScript sent to the client and improving performance.
Server Components are rendered on the server and sent to the client as a stream of HTML. This means:
Let’s see it in action:
Basic Server Component
// AsyncServerComponent.js
import { db } from './database';
async function AsyncServerComponent() {
const data = await db.query('SELECT * FROM users');
return (
<ul>
{data.map(user => (
<li key={user.id}>{user.name}</li>
))}
</ul>
);
}
export default AsyncServerComponent;
Notice how we’re directly querying the database? That’s the power of Server Components – no need for API routes or client-side data fetching!
Not all components need to be Server Components. You can (and should) mix and match:
Mixed Server and Client Components
// ServerComponent.js
import ClientComponent from './ClientComponent';
function ServerComponent({ data }) {
return (
<div>
<h1>Server-rendered content</h1>
<ClientComponent initialData={data} />
</div>
);
}
// ClientComponent.js
'use client';
import { useState } from 'react';
function ClientComponent({ initialData }) {
const [count, setCount] = useState(0);
return (
<div>
<p>Client-side interactivity: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
The 'use client'
directive tells React that ClientComponent
should be rendered on the client, allowing for interactivity.
Next.js 13+ embraces Server Components by default in the App Router. Here’s how you might structure a Next.js app with Server Components:
Next.js App with Server Components
// app/page.js
import { getPosts } from './lib/posts';
import PostList from './components/PostList';
export default async function Home() {
const posts = await getPosts();
return (
<main>
<h1>My Blog</h1>
<PostList posts={posts} />
</main>
);
}
// app/components/PostList.js
export default function PostList({ posts }) {
return (
<ul>
{posts.map(post => (
<li key={post.id}>{post.title}</li>
))}
</ul>
);
}
// app/lib/posts.js
export async function getPosts() {
// This could be a database query
return [
{ id: 1, title: 'Hello Server Components' },
{ id: 2, title: 'React is awesome' }
];
}
In this setup, Home
and PostList
are Server Components by default. They can fetch data directly without needing API routes.
One of the coolest features of Server Components is streaming. You can start sending HTML to the client before all the data is loaded:
Streaming with Suspense
import { Suspense } from 'react';
import PostList from './PostList';
export default function Blog() {
return (
<div>
<h1>My Blog</h1>
<Suspense fallback={<div>Loading posts...</div>}>
<PostList />
</Suspense>
</div>
);
}
This allows for a better user experience, as parts of the page can be interactive while others are still loading.
useState
or useEffect
. For interactive elements, use Client Components.Server Components are changing the game for React developers. They offer a new way to think about building applications, combining the best of server-side rendering with the interactivity of client-side React.
Key takeaways:
Now go forth and build some server-side awesomeness! 🚀
Want to dive deeper into the React ecosystem? Check out these mind-bending articles: