React context has a familiar API. You create an instance of your context in a parent component, provide a value, and then call a child component to consume it. In a modern React app, the client makes a request to the edge, which is turned into a React server components request and sent to regional compute. The returned serialized DOM is translated into HTML through server-side rendering and delivered to the client. The client fetches the JavaScript from the edge, hydrates the HTML into an interactive React app, and can perform server actions that mutate the database or file system.
Similarly, React context has a very familiar API. So, again, you import from the React module. You create an instance of your context. Within a parent component, you can provide a value and then call some child component, and then that child component can use a function to retrieve that value from the parent. Like I say, very similar to ALS, and in fact, there's basically just those three points, creating the instance, a parent that calls it with a value and a child that can consume it.
So, if we just sort of take a step back and remind ourselves of exactly how React app gets rendered these days, because it has changed a bit with React 18 and React components. So, this is just sort of an example of how you might render a modern React app.
The client makes a request to the edge. This then gets turned into an RSC, React server components request, and gets sent to some sort of regional compute. This is near to where your database or upstream or whatever it is happens to be. This then does some serious server things. This is maybe speaking to a file system or, like I say, a database or a SQLite thing or whatever it is. And this all happens far away from user in regional compute. The RC then is translated into some sort of like serialized DOM. It's a very interesting format. Basically it allows for streaming, that sort of thing. This goes back to the edge. The edge then turns this into HTML through server side rendering, and sends it back to the client.
The client has then got a HTML page, but they need to actually fetch the JavaScript to make it interactive. So, they request that from the edge. And because it's the edge, it just delivers the asset straight back to the client fast and speedy. Client then does client side hydration. That's actually turning the HTML that it has on the page at the moment into an actual interactive React app. And then we might actually want to take action. So, I might click submit on a form. And that's going to send a server action to over the network sending my form values. It's gonna be proxied through the edge and end up in the region. And again, that's going to be able to do some serious server things. That might be mutating my database or hitting the file system. Whatever.
Comments