Hi, everyone. I'm Julian, and yes, I do want to talk about signals. Now, JavaScript signals have had their fair share of attention and hype over the last few years. Especially in combination with the current TC39 proposal, I think it becomes more and more important for people to kind of be cross what signals are, how they work, and what problems they're trying to solve. But signals have also been part of, let's say, less positive conversations and discussions, especially in the React ecosystem around React and it not using signals. So I do want to touch on that. Why is React not using signals, what it's doing instead, but also why I think as a React developer, you should still follow the TC39 proposal, and I'm personally still hyped about it. Now, this is not going to be a talk about how signals are right, and if you're not using signals, you're wrong, or vice versa, like React is right, and if you are using signals, you're wrong. For me, this is not a right or wrong situation. So my talk really more aims towards trying to give everyone information about signals and how they work, so that you hopefully can go away and make more informed decisions for yourself and have more informed discussions. But also, and maybe more importantly, I do want to highlight the two different paradigms that have evolved in the JavaScript ecosystem and how we got there. So with all that out of the way, let's dive straight into it and let's talk about signals.
What are signals? Let's start there, just to get everyone on the same page. Signals are the idea of reactive variables in JavaScript, and what that means is we can take a look at a very simple example. We have three variables here, the counter, and then two derived values. So isEven is derived from counter and is true or false, and parity is derived from isEven and just turns it into string, even or odd. Now, whenever I reassign counter, which I can do because it's just a mutable variable, I have to remember to also reassign and recalculate all of those derived values, and that's obviously not great and that's tedious, and the idea of reactive variables is that I don't have to do that. So let's imagine a world, so this is not real JavaScript, this is a thought experiment and it has nothing to do with the signals proposal just yet, but let's imagine a world where we had a new assignment operator. So I chose the dollar equals instead of the normal equals, and that new assignment operator would tell the compiler that these are reactive variables. So we use a for isEven in parity here, counter can stay the same, and what we're telling the compiler is, hey, please, whenever any variables that we use in our calculation, whenever any of those change, please automatically recalculate this variable. That means if we change counter, we don't have to do anything else because the recalculation magically already happens through the compiler. Now, this is the core idea behind signals. Why would we want that?
Most of what we do as front-end developers is dealing with state and turning that state into UI. Now, when I started developing quite a while ago, the main paradigm around that was model view control or MVC, and the idea was that your view should always be a pure function of the model. In more modern terms, you can say your UI should always be the result of a pure function of your state. That sounds probably familiar, and the pure bit is really important here because basically what we want is if we put in the same state, we always want to get the same UI out of it. It just makes it very deterministic and easy to deal with. Now, how do we get from that to signals, which sounds more side-effecting? To understand that, we have to take a quick look at the history of reactivity on the web. Again, we have to start at the very beginning. We start with HTML and some kind of subset language.
Comments