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.
So let's understand what a Fibre object is, not the algorithm, the objects. So a Fibre is also a JavaScript object that contains information about a component, its input and its output. And it also corresponds to an instance of a component. There's quite a lot of fields on FibreNode. So this is not a comprehensive list by any means, but just to give you a general idea, you can see there's information about the component children, if it has any siblings, a pending updates type, pending props, pending state, work priority, and a bunch more. So when it comes to the Fibre algorithm, it has two phases, reconciliation and commit. Now, the reconciliation is interruptible, meaning we can pause and resume it whenever we want, and the commit is not interruptible, meaning once we start it, we can't stop it. We have to let it finish. So during the initial render, each React element that's being returned from the render function call is being converted into a Fibre. And when a React element is converted into a Fibre node for the first time, React uses that data, that Fibre that was created from the call. Essentially the React element is being converted to a Fibre, and then that Fibre is being put into a tree that is called current. So here you can see a specific example for the input element, but this, of course, happens for all the other nodes in the current tree as well.
Comments