top of page
  • Writer's pictureRevanth Reddy Tondapu

Part 7: Exploring React Server Components in Next.js


Exploring React Server Components in Next.js
Exploring React Server Components in Next.js

Next.js offers powerful features for building modern web applications, one of which is the use of React Server Components (RSC). This feature allows components to be rendered on the server, providing a more efficient and performant way to build applications. In this post, we'll delve into React Server Components, how they differ from traditional components, and how to use them in your Next.js projects.


What Are React Server Components?

React Server Components (RSC) are a new feature in React that allows components to be rendered only on the server. Unlike traditional components, they do not send any JavaScript code to the browser, which is why they are often referred to as having "Zero-Bundle-Size."


How React Server Components Work in Next.js

In Next.js, the new App Router uses React Server Components by default. This means that when you request a page, the server renders the components, and only the resulting HTML is sent to the browser. Here's a simple example to illustrate:

// HomePage.jsx
export default function HomePage() {
  console.log('[HomePage] rendering');
  return (
    <>
      <h1>Indie Gamer</h1>
      <p>Welcome to the Indie Gamer website!</p>
    </>
  );
}

When you load this page, the "[HomePage] rendering" message will appear in the server logs, but not in the browser console. This indicates that the component is being rendered on the server.


Comparing App Router and Pages Router

In older versions of Next.js using the Pages Router, components were rendered both on the server and in the browser. Let's see an example using the Pages Router:

// pages/index.jsx
export default function HomePage() {
  console.log('[HomePage] rendering');
  return (
    <>
      <h1>Indie Gamer</h1>
      <p>Welcome to the Indie Gamer website!</p>
    </>
  );
}

When this page is loaded, the HomePage component is rendered on the server and then re-rendered in the browser. This approach ensures that any client-side functionality works as expected, but it also means sending the component code to the browser, which can be less efficient.


Adding Client-Side Functionality

Sometimes, you need client-side functionality, such as displaying a popup or handling user interactions. In the Pages Router, you could use the useEffect hook to run code after the component is rendered:

import { useEffect } from 'react';

export default function HomePage() {
  useEffect(() => {
    window.alert('Welcome to my site!!!');
  }, []);

  return (
    <>
      <h1>Indie Gamer</h1>
      <p>Welcome to the Indie Gamer website!</p>
    </>
  );
}

In the App Router, however, trying to use useEffect directly in a Server Component will result in an error because server components do not support client-side functionality.


Switching to Client Components

To use client-side functionality in the App Router, you need to convert your Server Component to a Client Component by adding the use client directive at the top of the file:

// HomePage.jsx
'use client';

import { useEffect } from 'react';

export default function HomePage() {
  useEffect(() => {
    window.alert('Welcome to my site!!!');
  }, []);

  return (
    <>
      <h1>Indie Gamer</h1>
      <p>Welcome to the Indie Gamer website!</p>
    </>
  );
}

With this change, the component will be rendered both on the server and in the browser, allowing the useEffect hook to work as expected.


Benefits of React Server Components

  1. Efficiency: Server Components do not send their JavaScript code to the browser, reducing the overall bundle size and improving load times.

  2. Performance: By rendering components on the server, you can leverage server-side resources and reduce the computational load on the client.

  3. SEO-Friendly: Pre-rendered HTML is more easily indexed by search engines, improving your site's SEO.


Limitations of Server Components

While Server Components offer many benefits, they also have some limitations:

  • No Client-Side Hooks: You cannot use hooks like useEffect or useState in Server Components.

  • No Event Handlers: Event handlers like onClick are not supported in Server Components.

  • No Browser APIs: Server Components cannot access browser-specific APIs like window or document.


Conclusion

React Server Components provide a powerful way to build efficient and performant web applications by leveraging server-side rendering. In Next.js, you can use the App Router to take advantage of Server Components by default, and switch to Client Components when you need client-side functionality.

By understanding the differences between Server and Client Components and knowing when to use each, you can build faster, more efficient web applications that deliver a great user experience.

17 views0 comments

Comments


bottom of page