My name is Nicolas, and today, I'm going to talk about Fine-Grain Reactivity without the compiler this time. This is the last talk before the launch. After that, you will be able to lunch, so please stay.
But before we go into Fine-Grain Reactivity, we first need to understand what is Reactivity about and is React reactive today? In order to understand what is Reactivity about, I took this definition from the Vue ecosystem. When you modify the state, the Vue update, it makes state management simple and intuitive. We have another version of this one, which is just saying that the DOM or the Vue is a function of the state.
So the first question we should ask ourselves is, is React reactive or not? And in order to answer that question, a very simple thing to do is just to give it a try and to see if React is reactive. So in order to do that, I will consider this very simple application. It's made of three counters. So the first three lines are just counters. You can increment, decrement the counters, and you can see the value of the counter. At the end, you have the last line, and the last line is just the total of the three counters above.
If you were to build such application, you may come up with something like that, with three main components, one app component, one counter component, and a total. And obviously, within your app, you will use the counter component three times. But with that one, it doesn't work that well. You need to put some, I would say, interactivity for the users to be able to use your counters. So you will put some states directly within the counters, and with that, you are just able to play with each of the counters independently. You can increment, decrement your counters, and it will be fine for the users. But at some point, you need the total, and if you need the total, you need to know about the state of each of these counters directly within your total. And at this point, the usual trick is to move the states higher in the tree, and so we get the first three states directly in the app, and you just move the props down to the counters and to the total.
Now that we have everything, let's see how it works at the end. So let's see what is the code of this app component, because it is the main component in this whole application. So we'll just, this is like how your app component will be in that case. So my app component is made of three states. Each one has a value and a set value. And you can see that I pass the value plus the setter function directly to each of the counters. And at the end, you can see the total component, which is receiving the three values. This is a basic app.
On the right of the screen, you can see the app in action. So I'm just playing with this application.
Comments