So starting with fibres and a bit of an overview. The way I like to see fibres is let's think about a regular JavaScript function. So we have this add functions, it has two parameters, I'll just sum them. If you think of what a stack frame for that would look like, we would get something like this. We have a return, we have the function itself, we have a few parameters and the local variables being the numbers or results.
If we think about a React component, we can get to something similar. So we can think of a fibre in this way where we have our components instead of a function. Our props instead of our parameters and then our component state being our local variables. So to start, we can think of the fibre architecture as this React specific call stack model that basically gives full control over scheduling of what should be done. And, a fibre itself is basically a stack frame for a given React component. Okay, we got to this definition, we got to this parallel with regular stack frames, now we can start seeing fibres as units of work.
So let's think what happens with our components. So once our template goes through the JSX compiler, we end up with a bunch of elements that's what you note. What happens next is during reconciliation, the data from all of these elements is merged into three alpha fibre nodes, then we'll talk a bit more about them. And then, of course, depending on the type of, what is it, for example, it can be a function component, a class component, a suspense component, or whatever, so depending on the type of the thing, React itself needs to perform different types of work. So it's going to tag these. And then, each element is converted into this fibre node that we're talking about, that's going to describe for React what kind of work that needs to be done. So this leads us to this unit of work thing. And because fibres are units of work, it makes that convenient to track, schedule, pause, and abort specific types of work.
Now that we can abstract those in terms of units of work, we can also think about visualizing those units. And for this, I'd like to propose this first experiment that is inspecting elements. So let's take this simple app. I have just a few components with some local state and I have this button. I am incrementing and decrementing this local state and that's about it. If we start logging our fibres in the console, we are going to see this, a bunch of metadata about our components. And we'll see metadata about props, about state, about etc. And I know it was like really tiny but don't bother trying to read that. But I just wanted to see that there's a bunch of information there. And we can use, for example, this kind of information. So here's a piece of code where I'm using only five of these properties to iterate on those fibres.
Comments