And Now You Understand React Server Components

Rate this content
Bookmark

FAQ

The speaker of the talk at React Summit is Kent C. Dodds.

The main topic of Kent C. Dodds' talk at React Summit is React Server Components.

Kent C. Dodds initially felt hesitant about React Server Components because he felt that Remix already solved the same problems.

The purpose of building a framework based on React Server Components is to help people understand the building blocks of React Server Components.

Some rules include not using a bundler, TypeScript, or JSX, and avoiding dependencies except for official React stuff like React Error Boundary and Hano.js.

Using 'createElement' as 'h' is significant because it avoids the use of JSX, focusing on the core idea of React Server Components.

Kent C. Dodds explains that streaming allows different parts of the UI to be displayed as soon as they are ready, which improves performance.

UI updates are handled by making network requests to fetch updated content and replacing the existing content with the new content.

Kent C. Dodds uses caching and managing state for the current location and content to handle navigation and back button functionality.

More detailed information can be found in the open-source GitHub repository of the Epic Web Workshop, which is part of Epic React version 2.0.

Kent C. Dodds
Kent C. Dodds
27 min
18 Jun, 2024

Comments

Sign in or register to post your comment.

Video Summary and Transcription

In this Talk, Kent C. Dodds introduces React Server Components (RSCs) and demonstrates how to build them from scratch. He explains the process of integrating RSCs with the UI, switching to RSC and streaming for improved performance, and the benefits of using RSCs with async components. Dodds also discusses enhancements with streaming and server context, client support and loaders, server component rendering and module resolution, handling UI updates and rendering, handling back buttons and caching, and concludes with further resources for diving deeper into the topic.

1. Introduction to React Server Components

Short description:

Hey, React Summit! My name is Kent C. Dodds and I'm excited to give you this talk and now you understand React Server Components. We're going to build React Server Components from scratch. We're going to build a framework based on React Server Components, and this is one of the mechanisms that I typically use to help people understand the building blocks upon which they're building. Here are the rules, this is like a Legend of Zelda speedrun, you've got to have rules, so we're not going to use a bundler. We're not using any dependencies. I am assuming that you're already optimistic about RSEs, I'm not here to convince you that RSEs are awesome, you're willing to dive in for details later. You already know the basics of RSEs, so useClient is not a new concept for you. And then also you're smart enough to not try to do this in production, this, like I said, very sub-optimal. It's a single-page app, so we're going to start out with fully 100% single-page app, not even server-side generated or anything like that, just a server or a single-page app in the client.

Hey, React Summit! My name is Kent C. Dodds and I'm excited to give you this talk and now you understand React Server Components. Wish me luck! Now, if we were in person, I would ask you to wake up and stand up. If you're physically able, join us for some air squats. We're not in person, so I'm not going to make you do that. But if it's been a while since you've gotten your blood flowing, you should do that because your brain needs blood flow. We're going to skip that for today, though.

All right. So, back in December of 2020, when Server Components was announced, I remember thinking, feeling kind of funny about it. I said, everyone's super excited about React Server Components and I guess I am supposed to be, too, but I'm feeling really meh about it. Thing is, a few months ago, I would have been going bonkers over this stuff, but honestly, Remix solves the same problems already, so and then I had a thread of kind of why I was feeling the way I was feeling. Still a little bit optimistic, but just like I felt like the problems that Server Components were intended to solve didn't really apply to me as much. I was missing a couple of things that Server Components does that Remix won't be able to do without Server Components, and so please forgive me my hesitancy, but eventually I did come around to Server Components, and now I have actually built a framework based on Server Components. Mine isn't intended for production or anything, but yeah, this is kind of the transformation that I've had, and if you're familiar with this scene, this is where the hobbits make it back after their journey through Mordor and everything, and they're changed, and I kind of feel that way, too, after having delved into Server Components a bit, and now I can appreciate the value that there's there. I know maybe some of you are feeling this way, I don't understand React Server Components, and at this point I'm afraid to ask, but my job is to try and explain React Server Components in such a way that it's simple enough for you to understand. That's my goal, so wish me luck.

Alright, so we're going to build React Server Components from scratch. We're going to build a framework based on React Server Components, and this is one of the mechanisms that I typically use to help people understand the building blocks upon which they're building, so I don't expect you to actually do this, but hopefully by building a framework on top of React Server Components you have a much clearer distinction of what's React Server Components and what's a framework thing and whatever. So here are the rules, this is like a Legend of Zelda speedrun, you've got to have rules, so we're not going to use a bundler. Bundler would just distract us from the core idea of what is React Server Components. TypeScript as well, so we don't want to have any build tools at all, we're just going free without TypeScript, and not even JSX, so you're going to find some createElement as h, that's short for hyperscript, and yeah, so we're going to be using createElement, the createElement API directly. Luckily we're not spending a lot of time in the JSX stuff, so you're fine, we're not going to be doing any optimizations, there are plenty to be had, but this is not going to be an optimal thing, and we're also not using any dependencies. I don't want you to be distracted by all of the other extra stuff. We are basically no dependencies except for official React stuff, React Error Boundary, and Hano.js. A couple things I'm going to be taking for granted, I am assuming that you're already optimistic about RSEs, I'm not here to convince you that RSEs are awesome, you're willing to dive in for details later, so we're going to be glazing over a couple of things, and feel free to dive in later for details. You already know the basics of RSEs, so useClient is not a new concept for you. And then also you're smart enough to not try to do this in production, this, like I said, very sub-optimal.

Alright, with all of that established, here is the application that we're going to be working on for our example. It's a list detail view, it updates the URL and all of that stuff that you would expect. It's a single-page app, so we're going to start out with fully 100% single-page app, not even server-side generated or anything like that, just a server or a single-page app in the client.

2. Building React Server Components

Short description:

The project looks a little bit like this, we've got our database, we have our server, and our UI directory. We make a fetch to our API with the initial location and use a hook to get the data we need. On the server side, we have a static file server and an API endpoint for getting the ship by its ID. RSCs involve calling an API to get server-rendered UI. We swap out the API call for RSCs and combine the data with the UI on the server. To achieve composability, we make some diffs, import react-server-dom-esm-slash-client, and use it in our code.

The project looks a little bit like this, we've got our database, we have our server, this is the Hano.js server that has an endpoint for us, and then our UI directory is all the UI-related stuff. And then, here's a little bit of an intro to the code that we're going to be working with and modifying over time. We've got our initial location and our initial data promise, so we're making a fetch to our API with our initial location that will have the ship ID and any search parameters. We serialize that, or deserialize that as JSON, and then we're using the use hook with that promise, and we're destructuring out the data that we need for our app, and then this is our replacement for JSX, this H thing, create element as H, passing these props to our app component. Specifics are not critically important for you on what that app component does at this point. Also, because we're doing native stuff all over the place, we have an import map in our index HTML, so when we say import React and React DOM and React DOM client, we're pulling all of these from ESMSH, we're using the React 19 beta, and yeah, hopefully in the near future this will just be regular React. And then finally, our server side, we've got a static file server for all of our files, and then we have an API endpoint for getting the ship by its ID. Also, we're going to grab the search terms, so this is basically all of the data that you need for this page. That includes our ship ID, the search, the ship, and the ship results, which we send to the client on that initial page load. And then, we also have just kind of like a catch-all where we'll send that index HTML. So typical SPA situation without SSG.

Okay, great. So now, let's talk about it. Let's go from our API to RSC. So when we load the app, we're making a request to go get API slash ship ID with the search term that will give us all the data we need for our app. Ship ID, here's our search, and then here's the ship and here's the results for the ships that match this search term. This is pretty typical. Maybe you do a couple of different API calls, but ultimately you call an API to go get your data. So RSCs are actually not an enormously different thing. Instead of calling an API to get data, you're going to call an API to get RSCs. Now, I want to be clear that you don't have to do this via like a server interaction at runtime. You can do this at build time if you wanted to do a static site generation sort of thing, but we're going to be doing this at runtime. So our job is to swap out the slash API for slash RSC, and instead of getting the data here and then combining the data with the UI on the client, we're actually going to combine the data with the UI on the server and then just send the UI. Things get a little bit interesting when we want to compose the interactive bits with the non-interactive bits, and so that's why we're not just saying, hey, go get me the HTML and inner HTML everything. So we are going to be going a little bit above what you might think is the simplest way to do this, but the reason is because we want to have nice composability with our client-side code, which we'll get to later. So here's how we accomplish this. This is a bunch of diffs that we're going to be making. So first of all, for the client-side aspect, we've got our react-server-dom-esm-slash-client. We're going to need to import this, so that's why we're adding it to our import map, and this is going to be responsible for some aspect of this. And then here's where we're actually using that.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Simplifying Server Components
React Advanced Conference 2023React Advanced Conference 2023
27 min
Simplifying Server Components
Top Content
Watch video: Simplifying Server Components
React server components simplify server-side rendering and provide a mental model of components as pure functions. Using React as a library for server components allows for building a basic RSC server and connecting it to an SSR server. RSC responses are serialized virtual DOM that offload code from the client and handle interactivity. The client manifest maps serialized placeholders to real components on the client, enabling dynamic rendering. Server components combine the best of classic web development and progressive enhancement, offering the advantage of moving logic from the client to the server.
Exploring React Server Component Fundamentals
React Day Berlin 2023React Day Berlin 2023
21 min
Exploring React Server Component Fundamentals
Top Content
Watch video: Exploring React Server Component Fundamentals
This Talk introduces React Server Components (RSC) and explores their serialization process. It compares RSC to traditional server-side rendering (SSR) and explains how RSC handles promises and integrates client components. The Talk also discusses the RSC manifest and deserialization process. The speaker then introduces the Waku framework, which supports bundling, server, routing, and SSR. The future plans for Waku include integration with client state management libraries.
A Practical Guide for Migrating to Server Components
React Advanced Conference 2023React Advanced Conference 2023
28 min
A Practical Guide for Migrating to Server Components
Top Content
Watch video: A Practical Guide for Migrating to Server Components
React query version five is live and we'll be discussing the migration process to server components using Next.js and React Query. The process involves planning, preparing, and setting up server components, migrating pages, adding layouts, and moving components to the server. We'll also explore the benefits of server components such as reducing JavaScript shipping, enabling powerful caching, and leveraging the features of the app router. Additionally, we'll cover topics like handling authentication, rendering in server components, and the impact on server load and costs.
Server Components: The Epic Tale of Rendering UX
React Summit 2023React Summit 2023
26 min
Server Components: The Epic Tale of Rendering UX
Top Content
Watch video: Server Components: The Epic Tale of Rendering UX
This Talk introduces server components in React, which provide an intermediate format for rendering and offer advantages for both client-side and server-side rendering. Server components reduce bundle size on the client and improve search engine optimization. They abstract the rendering process, allowing for faster rendering and flexibility in choosing where to render components. While server components are still in the experimental stage, Next.js is a good starting point to try them out.
Batteries Included Reimagined - The Revival of GraphQL Yoga
GraphQL Galaxy 2021GraphQL Galaxy 2021
33 min
Batteries Included Reimagined - The Revival of GraphQL Yoga
Envelope is a powerful GraphQL plugin system that simplifies server development and allows for powerful plugin integration. It provides conformity for large corporations with multiple GraphQL servers and can be used with various frameworks. Envelope acts as the Babel of GraphQL, allowing the use of non-spec features. The Guild offers GraphQL Hive, a service similar to Apollo Studio, and encourages collaboration with other frameworks and languages.
React Server Components
React Day Berlin 2023React Day Berlin 2023
27 min
React Server Components
Watch video: React Server Components
React server components solve the problem of interrupting user interfaces caused by CPU-bound or network-bound web applications. They allow for read-only content to be rendered on the server and interactive elements to be shipped to the client, reducing code shipped and improving performance. Server-side rendering and server-side fetching improve the user experience by reducing delays and flash of unstyled content. Soft navigation with server components enables re-rendering without hard navigation, and using frameworks like Next.js helps with debugging and deployment challenges.

Workshops on related topic

Next.js 13: Data Fetching Strategies
React Day Berlin 2022React Day Berlin 2022
53 min
Next.js 13: Data Fetching Strategies
Top Content
WorkshopFree
Alice De Mauro
Alice De Mauro
- Introduction- Prerequisites for the workshop- Fetching strategies: fundamentals- Fetching strategies – hands-on: fetch API, cache (static VS dynamic), revalidate, suspense (parallel data fetching)- Test your build and serve it on Vercel- Future: Server components VS Client components- Workshop easter egg (unrelated to the topic, calling out accessibility)- Wrapping up
The Gateway to Backend: A Frontend Developer's Guide to Full-Stack Development
React Summit US 2023React Summit US 2023
160 min
The Gateway to Backend: A Frontend Developer's Guide to Full-Stack Development
Top Content
WorkshopFree
Amy Dutton
Amy Dutton
This workshop will guide you through the product development life cycle of creating a real-world web application. You will learn about React Server Components, building a design system within Storybook, and using frontend development to approach becoming a full-stack developer. The workshop will cover increasing confidence in your application with unit tests and implementing authentication and authorization. You'll have the opportunity to work through product features and examine a real-world RedwoodJS project, gaining valuable experience in real-world product development. RedwoodJS makes it simple to approach full-stack development, and this workshop will give you the skills you need to create your own real-world web applications.
Advanced Application Deployment Patterns with React Server Components (feat. a DIY RSC Framework)
React Summit US 2023React Summit US 2023
104 min
Advanced Application Deployment Patterns with React Server Components (feat. a DIY RSC Framework)
Top Content
WorkshopFree
 Greg Brimble
Greg Brimble
The developer ecosystem is always moving fast and this year has proved no exception. React Server Components can offer a significant improvement to developer experience and to application performance. But I think it's fair to say that this new server-first paradigm can be tricky to wrap your head around!In the first half of this workshop, we'll explore React Server Components from the ground-up: building our own mini meta-framework to help us understand how RSCs work. We'll discover exactly what is produced by an RSC build and we'll connect those pieces together to form a full application.Next, we'll deploy it! Cloudflare have also had a busy year too — Smart Placement, in particular, is a new technology that we've developed which fits the RSC model perfectly. We'll explore why that makes sense for our workshop app, and we'll actually deploy it onto the Cloudflare Developer Platform.Finally, we'll build out our app a little further, using D1 (our serverless SQL database) to really show off the React Server Component's power when combined with Smart Placement.You should come away from this workshop with a greater understanding of how React Server Components work (both behind-the-scenes and also how you as a developer can use them day-to-day), as well as insight into some of the new deployment patterns that are now possible after recent innovations in the platform space.
Building Reusable Server Components in NextJS
React Summit US 2023React Summit US 2023
88 min
Building Reusable Server Components in NextJS
Workshop
Will Bishop
Mettin Parzinski
2 authors
React continues to evolve their beta capability, React Server Components, and they're continuing to further develop them in partnership with frameworks like NextJS.In this workshop, attendees will learn what React Server Components are, how to effectively build and use them in NextJS, and focus on one of the major advantages of React/NextJS: reusability through components.We will also cover related beta technologies enabled by the `app` directory, such as nested layouts and server actions (alpha/experimental capability).Join us for this hands-on, 120 minute workshop!Technologies:
React, JavaScript/Typescript, NextJS, Miro
React Server Components Unleashed: A Deep Dive into Next-Gen Web Development
React Day Berlin 2023React Day Berlin 2023
149 min
React Server Components Unleashed: A Deep Dive into Next-Gen Web Development
Workshop
Maurice de Beijer
Maurice de Beijer
Get ready to supercharge your web development skills with React Server Components! In this immersive, 3-hour workshop, we'll unlock the full potential of this revolutionary technology and explore how it's transforming the way developers build lightning-fast, efficient web applications.
Join us as we delve into the exciting world of React Server Components, which seamlessly blend server-side rendering with client-side interactivity for unparalleled performance and user experience. You'll gain hands-on experience through practical exercises, real-world examples, and expert guidance on how to harness the power of Server Components in your own projects.
Throughout the workshop, we'll cover essential topics, including:- Understanding the differences between Server and Client Components- Implementing Server Components to optimize data fetching and reduce JavaScript bundle size- Integrating Server and Client Components for a seamless user experience- Strategies for effectively passing data between components and managing state- Tips and best practices for maximizing the performance benefits of React Server Components
Build a Custom Storefront on Shopify with Hydrogen
React Advanced Conference 2021React Advanced Conference 2021
170 min
Build a Custom Storefront on Shopify with Hydrogen
Workshop
Matt Seccafien
Cathryn Griffiths
2 authors
Hydrogen is an opinionated React framework and SDK for building fast, custom storefronts powered Shopify. Hydrogen embraces React Server Components and makes use of Vite and Tailwind CSS. In this workshop participants will get a first look at Hydrogen, learn how and when to use it, all while building a fully functional custom storefront with the Hydrogen team themselves.