1. Introduction to Fiber and Demo
Hi, I'm Elad Zamek, a software developer at Viva Systems. Today, we'll talk about Fiber, React's newest reconciliation algorithm introduced in React 16. We'll cover what React had before Fiber, why Fiber was necessary, and what we can achieve with it. Let's start with a quick demo. I have a simple app with an input field and a list. Whatever I type is rendered in the list. The list component waits for three milliseconds to simulate a heavy render.
Hi everyone, my name is Elad Zamek and I'm a software developer at Viva Systems in Toronto, Canada. Viva provides cloud-based products for the global life sciences industry and I currently work there as the lead UI developer for the Safety AI product.
Today, we'll be talking about Fiber, React's newest reconciliation algorithm that was introduced in React 16. We'll talk about what React had before Fiber, why Fiber was necessary, and some of the things we can achieve with Fiber. We have a lot to cover so let's get started.
I'll start with a quick demo here and as you can see, I have a pretty simple app that has an input field. Whatever I type into the input field is then being rendered. We have 80 elements in a list that's being rendered with the value that we typed into the field. If I type in a lot, we're going to get 80 elements with a text a lot. My slow list is a component that's being rendered. If I go into it, we can see it returns us an array of 80 elements of the list item component which is a component that waits for three milliseconds and then returns an LI element with the children prompt which is essentially the text that we passed. The reason that I'm waiting here for three milliseconds is just to simulate a component that's heavy that takes a long time to render because it's very complex.
2. Laggy Input Fields and Heavy Rendering
When typing into the input field, there is a noticeable lag before the value is rendered in the list. This is due to a complex heavy component, MySOLIST, that renders as the typing occurs. Many applications experience this issue with laggy input fields.
Now, if I type something into the input field, so let's say I'm going to type React conference, typing and stop typing now and then I can see the value in the input field and in the list. So I'll do that again, React conference, stop typing now, and then I see it. So I hope that you noticed that lag that I had. It was like about a second or two, between the moment I finished typing React conference into the input field, and the moment I saw the value being populated in the input field itself and being rendered on the list. And that's because when I typed in the value, I had another component, the MySOLIST component that rendered as I was typing it. And I'm sure that like most of you at least, I've seen this type of issue happening in your applications where you have like a laggy input field because there is another complex heavy component that needs to be rendered based on the value that you have in the input field or something similar to that.
3. Understanding the Laggy Experience
Let's take a look at our app and understand why it's laggy. The stack reconciler, used in React 15 and earlier, is a purely recursive algorithm. Whenever the app's state changes, the entire subtree is re-rendered immediately. This causes everything, including the slow list, to render, resulting in a laggy experience.
So let's take a look at our app just so I can understand why this is happening. And we're gonna look at it in the form of a tree connecting all of the components and see what's happening. So the stack reconciler is the implementation powering React 15 and earlier. I won't go too much into details about how this algorithm works because we are here to talk about the Fabry algorithm. But what's important to know is that for example, the state of app has changed. React will traverse this component tree recursively asking each component the way, what are you rendering to based on that state change. Then based on the result of the render function, it would create a virtual DOM and updated DOM with that result. So this reconciliation algorithm is a purely recursive algorithm. An update results in the entire subtree being re-rendered immediately. While this works well, it has some limitations. You can see here that the input element in blue and that's the one where fast response is important to us. We want the user to see what he or she types right as he or she types it. However, since the typing updates the state of the entire app component, everything renders including that red slow list and that's why everything seems so laggy.
4. Issues with Stack Reconciler
So what is really wrong with this stack reconciler? I have a quote here from Andrew Clark who's one of the team members of the React core team at Facebook. In UI, it's not necessary for every update to be applied immediately. In fact, doing so can be wasteful causing frames to drop and degrading the user experience. Now, with the old reconciliation algorithm, we couldn't break the work into incremental units. If React was going to walk the entire tree of components synchronously and perform work for each component, it may run over 60 milliseconds available for an application code to execute its logic.
Now, what do we mean when we refer to 60 milliseconds and why is this a problem with the recursive approach? Well, typically for a video to feel smooth and instantaneous to the human eye, the video needs to play at a rate of about 30 frames per second. Anything higher than that will give an even better experience. And this is actually one of the prime reasons why gamers prefer a higher frame rate for first-person shooter games where precision is very important. So having said that though, most devices these days refer to their screens at 60 frames per second, or in other words, with 60 frames per second, a new frame is displayed every 16 milliseconds. This number is very important because if the React render takes more than 16 milliseconds to render something on the screen, the browser will drop that frame and the user would get a lag experience similar to what we saw earlier. I don't know if you've ever seen a video that was created using 12 FPS or 10 FPS or 20 FPS, but it's like not as smooth as 30 and definitely not as 60 FPS.
5. Introducing Concurrent Mode in React
To make React faster, we need to solve two problems: long-running tasks causing frame drops and different tasks having different priorities. Introducing concurrent mode, an experimental feature in React, helps apps stay responsive and adapt to device capabilities. While not recommended for production, it's being used at Facebook. Check the official React docs for more details.
Now you might think, why not make React faster? Well, the problem in the demo that we saw, for example, was that in updates, such as the user input is stuck behind larger updates, such as the complex component tree, that was that slow list. That's user code, it's not part of React itself. So with everything I explained so far, we can formulate two problems that we have to solve in order to get two more responsive user interfaces. First one is that long-running tasks cause frame drops. And we need to make sure all of our tasks are small and can be completed within a couple of milliseconds so that we can run them within one frame. And then the second one is that different tasks have different priorities. So let's introduce concurrent mode. It is experimental and it's a set of new features that help React apps stay responsive and gracefully adjust to the user's device capabilities and network speed. So because it is experimental, things may change. It's not recommended to be used in production just yet, except for at Facebook where it is being used in production. And if you want more details, I would highly recommend checking out the official React docs.
7. React Fiber and the UseDeferredValue Hook
So React Fibre essentially eliminates the need to process updates in a synchronous recursive way. Instead, it enables React to take advantage of scheduling and prioritization, and that enables us to pause work and resume it later, to assign different priorities to different types of work, and reuse previously computed work or throw it away if it is no longer needed. So for example in our demo, React could keep the input field responsive to the user, meaning the user would see what he or she was typing right away since it would have a higher priority than the list of elements that needed to be rendered. Then when the user doesn't do anything, meaning doesn't block the main thread, React can render and display that list of elements.
So, that sounds cool, right? Well, let's see it in action. This is the same app that we had before. The only difference is that we imported the use deferred value hook here, and we're passing it the text state variable along an object with the timeout MS set to 2000. That's 2000 milliseconds, which is equal to two seconds. Now, we're passing the text state variable to our input field, because we wanted to be up to date with what we're typing, but to the MySlowList, we're passing that deferred text variable, because, well, you'll see. Now, let me type React Conference again. And you can't really see that, but as I'm typing, I'm seeing the characters being typed into the field. But the list is being left behind when it comes to what it renders. I'll do it again. See, finished typing, and now the list is slowly catching up on the rendering, right?
So the useDeferredValue is a hook that wraps a prop or state value and receives the maximum deferred time. This essentially tells React, it's okay for components depending on this value to be rendered at a later time. And it's commonly used to keep the interface responsive when you have something that renders immediately based on user input, just like in our demo here. So for example, with this code, it essentially says we need to show the text in the input field as soon as the user types it. We need to see exactly what they type, but when it comes to the list, it's okay if it takes up to two seconds to catch up on what it needs to render.
8. React Fiber and the Reconciliation Phase
React updates the DOM in one go, ensuring consistency. During the reconciliation phase, React builds a work in progress tree based on updates, containing the most up-to-date states and props for each component. This tree is not yet visible to the user. React processes components asynchronously, pausing and resuming based on available time and higher priority events. The second phase, the commit phase, is always synchronous.
React element being converted to a Fibre, which is being used to construct the current tree. And this tree is essentially, it reflects the current state of the UI that needs to be rendered. And it will be rendered, right? And the first render when we load our application.
Now let's talk about updates. When React starts working on updates, so, for example, a state change, it builds a work in progress tree that reflects the future state to be flushed to the screen based on that update. So as you can see in this case, it has the same fibres as the current tree. But let's say with this input element here, we can see on the left with the current tree, there's an effect tag of zero, meaning there's no update and the value is an empty string. But as we type, so let's say if we type alive, the first letter is E, we're gonna get a fibre with an effect tag of four, meaning there's an update pending and the value that it's gonna have is E. So all state and props update work is being performed on fibres from the work in progress tree. As React goes through the current tree for each existing fibre node, it creates an alternate node and all these nodes constitute the work in progress tree. The reason we need the work in progress tree is because we don't want to make changes to the DOM while we're computing the changes, unlike in the stack reconciler. Again, this slide just shows an example of how our input element is updated, but the same happens for all of the other fibres as well that have any updates. So the work in progress tree contains all of the most up-to-date states and props for each component, but it hasn't yet been flushed to the screen, but wait, does it contain all of the most up-to-date information on each component? Well, not really, because if you remember in our demo, we told React it's okay to delay the calculation of the slow list by at most two seconds. That means that other calculation work, like for our input element, is going to be prioritized and executed by the main thread, while other lower priority work, which is the mysoloist component in this case, will be queued for later because it has a lower priority. Only after the high priority work is done and displayed on the screen, only when we're done typing, because that's the high priority work, then React will move on to the lower priority work and calculate it. And again, that's the mysoloist component. So React has processed the app component and its children, except for mysoloist. And now it's done with the render phase, the reconciliation phase. On the right is the new tree that needs to be flushed to the screen. It can be processed immediately after the render phase or the reconciliation phase, it's the same thing, or picked up later when React is given time by the browser's main thread. In our case, since the two seconds haven't passed yet, let's say React has time to commit these changes to the screen. And of course, these changes being our typing into the input field. Now one of React's core principles is consistency. React always updates the DOM in one go, it doesn't show partial results, right? The work in progress tree serves as a draft that's not yet visible to the user, so that React can process all components first and then flush the changes to the screen. It's important to understand that the work during this first phase, the reconciliation phase, can be performed asynchronously. React can process one or more Fiber nodes depending on the available time, then stop to stash the work done and yield to some higher priority event if there is any. It then continues from where it had left off. But sometimes though, it may need to discard the work done and start from the top again. These pauses are made possible by the fact that the work performed during this phase doesn't lead to any user visible changes like DOM updates. In contrast, the second phase, the commit phase, is always synchronous.
9. Commit Phase and Fiber Benefits
The commit phase is where React iterates through the effect list and commits the changes to the DOM. After this phase, the work in progress tree has an up-to-date version of the app's state. The use deferred value hook addresses the problems of frame drops and different task priorities. However, concurrent mode features are not yet stable and should not be used in production. Fiber improves React apps by breaking work into small units called Fibers, allowing for pausing and resuming. For more information, refer to the React official docs and explore the React code.
And of course, that's because the work performed during this phase does lead to changes visible to the user, like DOM updates, and that's why React has to do it in a single pass. We cannot pause, show partial results and resume it. It has to go in a single pass.
So, the commit phase. Activities like mutating the DOM or calling lifecycle methods are considered effects and they are stored in the list called the effect list. In our example, the properties of both input and app change. So, their corresponding effects are stored here in this list. And React essentially and the commit phase would iterate this list and commit the changes to the DOM. So, if you remember the input field, we need to change it because we're showing something in that input field. But then also, the app state, we need to change its state as well. So, both of these components, those elements, have effects waiting to be committed to the DOM.
This also means, after all this work is done, the work in progress tree has a more up-to-date version of the state of the app, right? So, React also swaps the root pointers of the current tree and the work in progress tree, thereby effectively swapping the current tree with a draft tree it built up based on the state update we had.
So, to conclude, if you remember, at the beginning of the presentation, I formulated two problems that we have to solve in order to get more responsive user interfaces. The first one was that long-running tasks cause frame drops. And the second one was that different tasks have different priorities. We address these by using the use deferred value hook, which takes advantage of the fiber algorithm and essentially, it assigned a different priority for each task and solved our frame dropping issue we had at the beginning of the presentation. It's also again, important to mention that these concurrent mode features are not yet available in the stable release. And in order to use them, you'd have to install an experimental build of React, which is of course, not recommended to use in production since there are still some bugs and missing features.
So, Fiber will make React apps better in the near term by allowing higher priority updates to jump in front of low priority updates. And it does this by breaking up the work into small units of work called Fibers, which could be paused and resumed later. There's a lot more to talk about and mention when it comes to how Fiber works. So if you are interested in Fiber and all the experimental features of concurrent mode, I would highly encourage you to read more about it. The React official docs are a great resource as well as looking into the React code itself. I hope this presentation gave you a good idea of what's possible with Fiber and got you a little bit excited for the future. I personally think that the combination of Fiber and concurrent mode delivers a very bright future for React, where we as developers could achieve things we couldn't have before and by that enable a better user experience in our applications.