Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Carl Mungazi
Carl Mungazi

Posted on

     

A journey through ReactDOM.render

This was originallyposted on Medium

ReactDOM is an object which exposes a number of top-level APIs. According to the docs, it provides‘DOM-specific methods that can be used at the top level of your app and as an escape hatch to get outside of the React model if you need to’. One of those methods isrender().

Some background notes

At the core of this re-write is a data structure called afiber, which is an object that is mutable and holds component state and DOM. It can also be thought of as a virtual stack frame. Fiber’s architecture is split into two phases:reconciliation/render andcommit. Over the course of this article we will touch upon some of its aspects but more extensive explanations can be found here:

The journey begins

ReactDOM.render() actually wraps another function and invokes it with two additional arguments, so its declaration is simple. Below is the wrapped function:

functionlegacyRenderSubtreeIntoContainer(parentComponent,children,container,forceHydrate,callback){...letroot=container._reactRootContainer;if(!root){// Initial mountroot=container._reactRootContainer=legacyCreateRootFromDOMContainer(container,forceHydrate);if(typeofcallback==='function'){constoriginalCallback=callback;callback=function(){constinstance=DOMRenderer.getPublicRootInstance(root._internalRoot);originalCallback.call(instance);};}// Initial mount should not be batched.DOMRenderer.unbatchedUpdates(()=>{if(parentComponent!=null){root.legacy_renderSubtreeIntoContainer(parentComponent,children,callback,);}else{root.render(children,callback);}});}else{...}returnDOMRenderer.getPublicRootInstance(root._internalRoot);}
Enter fullscreen modeExit fullscreen mode

The first thing React does is create thefiber tree for our application. Without this, it cannot handle any user updates or events. The tree is created by callinglegacyCreateRootFromDOMContainer, which returns the following object:

{containerInfo:div#rootcontext:nullcurrent:FiberNode{tag:3,key:null,/*…*/}didError:falseearliestPendingTime:0earliestSuspendedTime:0expirationTime:0finishedWork:nullfirstBatch:nullhydrate:falseinteractionThreadID:1latestPendingTime:0latestPingedTime:0latestSuspendedTime:0memoizedInteractions:Set(0){}nextExpirationTimeToWorkOn:0nextScheduledRoot:nullpendingChildren:nullpendingCommitExpirationTime:0pendingContext:nullpendingInteractionMap:Map(0){}timeoutHandle:-1}
Enter fullscreen modeExit fullscreen mode

This is called afiber root object. Every React app has one or more DOM elements that act as containers and for each of these containers, this object is created. It is on this object we find a reference to our fiber tree via thecurrent property, and its value is:

{actualDuration:0actualStartTime:-1alternate:nullchild:nullchildExpirationTime:0effectTag:0elementType:nullexpirationTime:0firstContextDependency:nullfirstEffect:nullindex:0key:nulllastEffect:nullmemoizedProps:nullmemoizedState:nullmode:4nextEffect:nullpendingProps:nullref:nullreturn:nullselfBaseDuration:0sibling:nullstateNode:{current:FiberNode,containerInfo:div#root,/*…*/}tag:3treeBaseDuration:0type:nullupdateQueue:null}
Enter fullscreen modeExit fullscreen mode

This is afiber node and it is found at the root of every React fiber tree (this is the function which creates this object). This node is actually a special type of fiber node called aHostRoot node and it acts as a parent for the uppermost component in our application. We know it is aHostRoot node because the value of itstag property is3 (you can find the full list of fiber node typeshere). Notice how at this stage, a lot of the properties, especiallychild, arenull. We will come back to this later.

After creating the tree, React checks if we provided a callback as the third argument of the render call. If so, it grabs a reference to the root component instance of our application (in our case, it is the<h1> element) and then makes sure our callback will be called on this instance later on.

Into the thick of it

All the stuff that has happened prior to this point is preparation for the work that will render our application on screen. So far, we have a tree but it does not resemble our UI. This problem is solved with the following code:

// Initial mount should not be batched.unbatchedUpdates(function(){if(parentComponent!=null){root.legacy_renderSubtreeIntoContainer(parentComponent,children,callback);}else{root.render(children,callback);}});
Enter fullscreen modeExit fullscreen mode

Root is an object with only one property (the property_internalRoot which holds a reference to the root fiber object). It was created by callingnew on theReactRoot function, so if you look up its internal[[Prototype]] chain you will find the following method:

ReactRoot.prototype.render=function(children,callback){varroot=this._internalRoot;varwork=newReactWork();callback=callback===undefined?null:callback;{warnOnInvalidCallback(callback,'render');}if(callback!==null){work.then(callback);}updateContainer(children,root,null,work._onCommit);returnwork;}
Enter fullscreen modeExit fullscreen mode

Remember ourHostRoot fiber node earlier with all thenull values? In the function above, we can access it viaroot.current. AfterupdateContainer() has finished its work, this is what it looks like:

{actualDuration:6.800000002840534actualStartTime:95592.79999999853alternate:FiberNode{tag:3,key:null,/*…*/}child:FiberNode{tag:5,key:null,/*…*/}childExpirationTime:0effectTag:32elementType:nullexpirationTime:0firstContextDependency:nullfirstEffect:FiberNode{tag:5,key:null,/*…*/}index:0key:nulllastEffect:FiberNode{tag:5,key:null,/*…*/}memoizedProps:nullmemoizedState:{element:{/*…*/}}mode:4nextEffect:nullpendingProps:nullref:nullreturn:nullselfBaseDuration:2.5999999925261363sibling:nullstateNode:{current:FiberNode,containerInfo:div#root,/*…*/}tag:3treeBaseDuration:3.1999999919207767type:nullupdateQueue:{baseState:{/*…*/},firstUpdate:null,/*…*/}}
Enter fullscreen modeExit fullscreen mode

And the value in the child property is now a fiber node for the<h1> element:

{actualDuration:4.100000005564652actualStartTime:95595.49999999581alternate:nullchild:nullchildExpirationTime:0effectTag:0elementType:"h1"expirationTime:0firstContextDependency:nullfirstEffect:nullindex:0key:nulllastEffect:nullmemoizedProps:{children:"Hello, world!"}memoizedState:nullmode:4nextEffect:FiberNode{tag:3,key:null,/*…*/}pendingProps:{children:"Hello, world!"}ref:nullreturn:FiberNode{tag:3,key:null,/*…*/}selfBaseDuration:0.5999999993946403sibling:nullstateNode:h1tag:5treeBaseDuration:0.5999999993946403type:"h1"updateQueue:null}
Enter fullscreen modeExit fullscreen mode

As you can see, the fiber tree now reflects the UI we want rendered. How did that happen? Below is a summary of the steps which took place in order to get us to this stage:

Schedule the updates

React has an internalscheduler which it uses for handlingco-operative scheduling in the browser environment. This package includes apolyfill forwindow.requestIdleCallback, which is at the heart of how React reconciles UI updates. During the process each fiber node is given anexpiration time. This is a value which represents a time in the future thework (the name given to all the activities which happen during reconciliation) being done should be completed.

This calculation involves checking if there is any pending work with a higher priority as well as the nature of the work currently going on. In our case, it is a synchronous update but in other scenarios it could be interactive or asynchronous. Examples of low priority updates include network responses or clicking a button to change colours. High priority updates include user input and animations.

Create an update queue

In our example, we only have one fiber tree and since it has no update queue, one has to be created and assigned to theupdateQueue property. This queue is created with thecreateUpdateQueue function and it takes the fiber node'smemoizedState as its argument. Memoized state refers to the state used to create the node's output. The queue is a linked list of prioritised updates and like fiber trees, it also comes in pairs. Thecurrent queue represents the visible state of the UI.

Create the work-in-progress tree and do work on it

When the work is actually being performed, React checks its internal stack to determine whether it is starting with a fresh stack or resuming previously yielded work. Soon after this check it uses a technique known asdouble buffering tocreate theworkInProgress tree. React works with two fiber trees — one namedcurrent which holds the current state of the UI and another namedworkInProgress which reflects the future state. Every node in thecurrent tree has a corresponding node in theworkInProgress tree created from data during render.

Once theworkInProgress is rendered on the screen, it becomes the newcurrent tree. TheworkInProgress tree also has an update queue but it can be mutated and processed asynchronously before being rendered.

DOM element creation

Thefiber node for the<h1> element is created during the work on theworkInProgress tree. A few calls later,this function uses the DOM API to create our<h1> element, which so far has been a React element object. When the application has rendered, you can access the element by typingdocument.querySelector('h1'). If you check its properties, you will find one beginning with__reactInternalInstance$. This property holds a reference to the element's fiber node. React alsoassigns the element's text content and then laterappends it to our root DOM element. By the time its appended, React has already entered itscommit phase.

Accessing React Element via the DOM

And that is all there is to it

In our example we have rendered the most basic of UIs but React has done a lot of work to prepare and display it. Like most frameworks created for building complex applications, it does this work to prepare for all the eventualities associated with such a task. And with the example application being static, we have not touched on any of the code involved in state updates, lifecycle hooks and the rest of it.

As this is my first time doing a deep dive on React, I am sure I have missed things out or incorrectly explained others, so please, leave your feedback below!

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
zurezsgig profile image
zurez-sgig
Each day I realise, I know nothing. Each day I learn something new.
  • Joined

Thank you!

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Frontend Dev with a penchant for reading source code
  • Joined

More fromCarl Mungazi

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp