And Now You Understand React Server Components

Rate this content
Bookmark
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
Server Components are arguably the biggest change to React since its initial release but many of us in the community have struggled to get a handle on them. In this talk we'll try to break down the different moving parts so that you have a good understanding of what's going on under the hood, and explore the line between React and the frameworks that are built upon it.
Exploring React Server Component Fundamentals
React Day Berlin 2023React Day Berlin 2023
21 min
Exploring React Server Component Fundamentals
Top Content
I've been developing a minimalistic framework for React Server Components (RSC). This talk will share my journey to deeply understand RSC from a technical perspective. I'll demonstrate how RSC features operate at a low level and provide insights into what RSC offers at its core. By the end, you should have a stronger mental model of React Server Components fundamentals.
Server Components: The Epic Tale of Rendering UX
React Summit 2023React Summit 2023
26 min
Server Components: The Epic Tale of Rendering UX
Server components, introduced in React v18 end these shortcomings, enabling rendering React components fully on the server, into an intermediate abstraction format without needing to add to the JavaScript bundle. This talk aims to cover the following points:1. A fun story of how we needed CSR and how SSR started to take its place2. What are server components and what benefits did they bring like 0 javascript bundle size3. Demo of a simple app using client-side rendering, SSR, and server components and analyzing the performance gains and understanding when to use what4. My take on how rendering UI will change with this approach
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
Server Components are the hot new thing, but so far much of the discourse around them has been abstract. Let's change that. This talk will focus on the practical side of things, providing a roadmap to navigate the migration journey. Starting from an app using the older Next.js pages router and React Query, we’ll break this journey down into a set of actionable, incremental steps, stopping only when we have something shippable that’s clearly superior to what we began with. We’ll also discuss next steps and strategies for gradually embracing more aspects of this transformative paradigm.
Batteries Included Reimagined - The Revival of GraphQL Yoga
GraphQL Galaxy 2021GraphQL Galaxy 2021
33 min
Batteries Included Reimagined - The Revival of GraphQL Yoga
The Guild has recently released Envelop - a new, modern GraphQL Server Framework and plugin system. In this talk I’ll share a brief overview of Envelop and why you should probably upgrade your existing GraphQL server to it.
React Server Components
React Day Berlin 2023React Day Berlin 2023
27 min
React Server Components
React Server Components are a newer way of working with React that is widely adopted in frameworks like Next.js. In this talk, we will look under the surface of how they work and are executed on the server side, and how they fit with server rendering and traditional React apps.

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
React Server Components Unleashed: A Deep Dive into Next-Gen Web Development
React Advanced Conference 2023React Advanced Conference 2023
153 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
Workshop level: 
No matter your current level of React expertise, this workshop will equip you with the knowledge and tools to take your web development game to new heights. Don't miss this opportunity to stay ahead of the curve and master the cutting-edge technology that's changing the face of web development. Sign up now and unleash the full power 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.
Build a Product Page with Shopify’s Hydrogen Framework
React Advanced Conference 2022React Advanced Conference 2022
81 min
Build a Product Page with Shopify’s Hydrogen Framework
WorkshopFree
David Witt
David Witt
Get hands on with Hydrogen, a React-based framework for building headless storefronts. Hydrogen is built for Shopify commerce with all the features you need for a production-ready storefront. It provides a quick start, build-fast environment so you can focus on the fun stuff - building unique commerce experiences. In this workshop we’ll scaffold a new storefront and rapidly build a product page. We’ll cover how to get started, file-based routing, fetching data from the Storefront API, Hydrogen’s built-in components and how to apply styling with Tailwind.You will know:- Get started with the hello-world template on StackBlitz- File-based routing to create a /products/example route- Dynamic routing /products/:handle- Hit the Storefront API with GraphQL- Move the query into the Hydrogen app- Update the query to fetch a product by handle- Display title, price, image & description.- Tailwind styling- Variant picker and buy now button- Bonus if there’s time: Collections page
Prerequisites: - A Chromium-based browser (StackBlitz)- Ideally experience with React. A general web development background would be fine.