The first challenge is a simple hello world, we start easily. So we try to use the React Server Components technology but without using a server. So the challenge is called React Serverless Components, this is our first meaning for the acronym. So we want to create a very simple hello world, you can see it on the right, this is the final result. And on the left you can find some very simple code that you can run on a client, you don't need a server for this. And this code what it's basically doing is something that you can do with the RSC technology, that is loading some content that is pre-generated somewhere else, maybe on a server, I don't know, but for now we don't care about the server.
We can fetch this content from somewhere, then we can use a new function that is available in React 19, in the React Server Components package, obviously, that is called createFromFetch, that takes the content fetched and decodes in some way so that it can become something that we can render from React. There is also another thing that is new in React 19, a new feature in React 19, a new hook called use, maybe they finished the names for the hooks, so they went for the most simple one, it's called simply use. You may think you can use everything with use, but it's not the case, you can basically use any kind of resource, let's call them, in particular for now, it's used for using promises and context, so a simple way to get some remote resources, promises, and use it with a synchronous syntax instead of using async await and the usual stuff.
Now that we have the decoded stuff, we can render it using the usual render method from React DOM, there is nothing new in it, but maybe you are curious to understand what's this pre-generated content we are talking about. It's basically something like this. This is a new protocol that the React team introduced with RSE, it's called ReactFlight, this is the reason I told you remember the flight word, because it's important. It's a protocol that is used to serialize stuff so that you can send it from one place to another, for example from a server to a client, so you can generate the content in some place, send it somewhere else, and use it for example to share the rendering job between a server and a client. It's similar to JSON but it's not really JSON, we'll see a little bit more how it looks later. So with this we can imagine a workflow made like this one, for now we know how to deserialize this kind of content, using the createFromPatch function, we don't know yet how to serialize something on the server side, we'll see it later.
Okay, I can say that the first challenge is completed, so let's go quickly to the second one. It's not really that different, so I call it the first challenge and a half, because it's not really very different from the first one, so it was not good to call it second. The acronym in this case is translated as replaceable static content, because we want to add an additional step, what if we want to load more different content using RSC, not just a single page, we want to switch between different kind of contents. That's absolutely possible, the example it's the one you see on the right. Don't worry about the code, because there is a GitHub repository with all the snippets of code that I'm showing, I will give you the link later.
So with this example we don't use anything new from React 19, because the only thing we need to do is use the useState hook, for example, to store our content somewhere, like we are used to do. For example, we can store the result of the createFromPatch function that we have seen we have seen from the previous example, and just pass it down as a property to a non-sting or a root component, like in this case, and use it there. So our root component is in reality a very dynamic one, because it doesn't render anything from its own, it's just getting the content from somewhere else.
Something more about react-flight, because we are probably curious, we have already seen that in react-flight we can include some representation of our DOM in a sort of JSON-like syntax. It's not really JSON but it resembles it. But the other important thing is that content is not limited to just one row of stuff. We can have many pieces that are called chunks, each one has a prefix that is a key associated to this chunk, because an important property of react-flight is the ability to compose different chunks together, and the way you compose them is to reference other chunks with this $key syntax. So also react-flight is composable, like all the react libraries aim to be. So what can we think to do with all of these things that we have seen so far? Maybe static site generation, we can generate our react-flight payload somewhere and use it from a client without having a real server available. But the same is possible with the old HTML, so why do we need RLC to do that? Probably this is not a very strong selling point for RLC, but what I can tell you for now is that the react-flight protocol is a rich protocol, it can share between server and clients a lot of things more than simple HTML, so we will see probably later if this is something that we can use properly.
Comments