spinny:~/writing $ vim mastering-react-server-components.md
1~2React Server Components (RSC) have fundamentally changed how we build React applications. By moving rendering logic to the server, we can significantly reduce the JavaScript bundle sent to the client, leading to faster load times and better user experiences. In this article, we'll explore advanced patterns and best practices for leveraging RSCs in 2026.3~4## What are Server Components?5~6Traditionally, React components were rendered entirely on the client (CSR) or pre-rendered on the server as HTML (SSR) and then hydrated on the client. **Server Components** allow us to render components _exclusively_ on the server. Their code is never sent to the browser.7~8### The Benefits9~101. **Zero Bundle Size**: Dependencies used in Server Components (like markdown parsers or heavy date libraries) stay on the server.112. **Direct Backend Access**: You can query your database directly inside your component without exposing API endpoints.123. **Automatic Code Splitting**: Client components imported server-side are automatically code-split.13~14## Data Fetching Pattern15~16With RSC, you can make your components `async` and await data directly.17~18```tsx19// app/users/page.tsx20import { db } from '@/lib/db';21~22export default async function UsersPage() {23 const users = await db.user.findMany();24~25 return (26 <main>27 <h1>Users</h1>28 <ul>29 {users.map((user) => (30 <li key={user.id}>{user.name}</li>31 ))}32 </ul>33 </main>34 );35}36```37~38### Suspense for Streaming39~40Since data fetching can be slow, wrap your components in `<Suspense>` to show a fallback UI immediately while the data loads.41~42```tsx43import { Suspense } from 'react';44import UserList from './UserList';45import LoadingSkeleton from './LoadingSkeleton';46~47export default function Page() {48 return (49 <section>50 <h1>My Users</h1>51 <Suspense fallback={<LoadingSkeleton />}>52 <UserList />53 </Suspense>54 </section>55 );56}57```58~59## Interleaving Server and Client Components60~61One of the most confusing aspects is how to combine Server and Client components.62~63**Rule of Thumb**: Server Components can import Client Components. Client Components _cannot_ import Server Components directly. However, you can pass a Server Component as `children` to a Client Component.64~65### The "Children" Pattern66~67```tsx68// ClientComponent.tsx69'use client';70~71import { useState } from 'react';72~73export default function ClientWrapper({ children }) {74 const [count, setCount] = useState(0);75~76 return (77 <div onClick={() => setCount((c) => c + 1)}>78 Count: {count}79 {children} {/* This can be a Server Component! */}80 </div>81 );82}83```84~85```tsx86// ServerPage.tsx87import ClientWrapper from './ClientWrapper';88import ServerContent from './ServerContent';89~90export default function Page() {91 return (92 <ClientWrapper>93 <ServerContent /> {/* This works perfectly */}94 </ClientWrapper>95 );96}97```98~99## Common Pitfalls100~1011. **Using Context in Server Components**: Context is a client-side concept. If you need to share data server-side, pass it as props or use a specialized request-scope cache.1022. **Adding Event Handlers**: You cannot add `onClick` or `onChange` to a Server Component. That logic belongs in a generic Client Component leaf node.103~104## Conclusion105~106React Server Components are not just a feature; they are a paradigm shift. By understanding the boundary between server and client, you can build applications that are both highly interactive and incredibly fast.107~
NORMAL · mastering-react-server-components.md [readonly]107 lines · :q to close