Maison >interface Web >js tutoriel >Comprendre les composants du serveur React : un guide pratique
React Server Components (RSC) are revolutionizing how we approach server-side rendering in React applications. This guide will walk you through what they are, their benefits, and how to implement them in your projects.
React Server Components are a new type of component that runs exclusively on the server. They render on the server and send their output to the client, combining the benefits of server-side rendering with client-side React interactivity.
Key features:
As of 2024, React Server Components are best used with frameworks like Next.js. Here's a quick setup:
npx create-next-app@latest my-rsc-app cd my-rsc-app
Choose to use the App Router when prompted.
Create a Server Component in app/ServerComponent.tsx:
async function getData() { const res = await fetch('https://api.example.com/data') return res.json() } export default async function ServerComponent() { const data = await getData() return <div>{data.message}</div> }
import ServerComponent from './ServerComponent' export default function Home() { return ( <main> <h1>Welcome to React Server Components</h1> <ServerComponent /> </main> ) }
npm run dev
Let's create a more complex example - a blog post list fetching data from a CMS:
// app/BlogList.tsx import { getBlogPosts } from '../lib/cms' export default async function BlogList() { const posts = await getBlogPosts() return ( <ul> {posts.map(post => ( <li key={post.id}> <h2>{post.title}</h2> <p>{post.excerpt}</p> </li> ))} </ul> ) } // app/page.tsx import BlogList from './BlogList' export default function Home() { return ( <main> <h1>Our Blog</h1> <BlogList /> </main> ) }
In this example, BlogList fetches data directly from the CMS on the server, improving performance and simplifying the client-side code.
// ClientComponent.tsx 'use client' import { useState } from 'react' export default function LikeButton() { const [likes, setLikes] = useState(0) return <button onClick={() => setLikes(likes + 1)}>Likes: {likes}</button> } // ServerComponent.tsx import LikeButton from './ClientComponent' export default function BlogPost({ content }) { return ( <article> <p>{content}</p> <LikeButton /> </article> ) }
import { Suspense } from 'react' import BlogList from './BlogList' export default function Home() { return ( <main> <h1>Our Blog</h1> <Suspense fallback={<p>Loading posts...</p>}> <BlogList /> </Suspense> </main> ) }
'use client' export default function ErrorBoundary({ error, reset }) { return ( <div> <h2>Something went wrong!</h2> <button onClick={reset}>Try again</button> </div> ) } // In your page file import ErrorBoundary from './ErrorBoundary' export default function Page() { return ( <ErrorBoundary> <BlogList /> </ErrorBoundary> ) }
React Server Components offer a powerful approach to building performant, SEO-friendly, and secure web applications. They represent an exciting direction for React development, allowing for server-side data fetching and rendering while maintaining the interactivity of client-side React.
As you explore Server Components, remember they're not a replacement for client-side React, but a complementary technology. Use them where they make sense in your application architecture.
We encourage you to experiment with Server Components in your projects and stay tuned for further developments in this exciting space!
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!