Instead of re-running the whole component any time state changes, it's just going to re-run the part of the page that cares about that state. It's just going to re-render the quantity every time quantity updates. And it won't update product images and shipping text, yay, but it won't even update price. You have to be very specific about what parts of the page re-render.
So in Solid, for example, if you want to declare dependencies, like price depends on quantity, you can just create an execution function. And, the Solid Compiler is smart enough to say, alright, this is a function now, I'm going to trace the dependencies inside of this function, quantity, and I'm going to re-run that function any time one of those dependencies changes. So not even a dependency array, so it's just smart. Now that we've made this function, we can see that Solid will update price. But unlike the React problem, we're not going to reload anything else that doesn't depend on quantity, because again, you opt into reactivity with functions. So that's how the whole framework works, and it's a really nice primitive. And, we'll see this in modern examples like Svelte, and Vue, and Angular, they all are based on this model of where the initial state is going to be run once, and then we only rerun wherever state is used. So we rerun the JSX element right here instead of rerunning your function top to bottom as you would in React. Cool little trick, cool little mental model.
And Svelte does the same thing. Using the dollar sign operator, and this is changing in future versions, apparently to a state operator, that will rerun price whenever quantity changes. Same sort of flag to say, hey compiler, look at the dependencies, re-render this stuff whenever those dependencies change. They all kind of work like this, and we're converging on a model of signals in order to manage this.
So, lastly I'll leave you with a little demo, looks like I have about two minutes, but I'll you know, speed run this, see what we can get to. It's on a repo called simple-rsc. So if after the talk, you want to try out this tool, even contribute improvements to it, I definitely welcome it. But it's a basic benchmark that shows you how React Server Components work outside of Next.js. So you can understand the mental model of Server Components, what they really do. So, I have that code pulled up right here, we're going to zoom in for you all just a little bit. And we can see we have our server root, this is just a page that's rendering, don't worry too much about that. And we're just rendering the static text, abramix. I built this with Dan Abramov on Stream. It's a Spotify clone, so Abramov, Abramix. I thought it was funny, okay? I thought it was funny. But yeah. It's just rendering this static text right here.
Comments