# Understanding The React Source Code - Initial Rendering (Class Component) IV

We have completed the rendering process of a simple component. This time we are going to explore more ramifications of this process by discussing how a class component (a typical one we might use in everyday development) is rendered.

Files used in this article: the same as post one and two

I use {} to reference the previous post that is relevant to the methods (or logic process) being discussed.

The component named App is similar to what I gave in the beginning of post one, in which we considered it too complex for beginners. But since we have leveled-up a bit, it does not look that daunting anymore.

As mentioned, the component above is rendered using:

Now the babeled code:

Here we consider Component a common base class, as other methods will not be used in this post.

This time we can fast forward the logic that is shared with simple component.

# Construct the top level wrapper ReactCompositeComponent[T]

The designated data structure:

This step is almost the same as that in simple component rendering, so I will give a brief description only, it

1) creates ReactElement[1] using ReactElement.createElement(type, config, children) (This time App is passed to type, and config, children are null);

1. creates ReactElement[2] in _renderSubtreeIntoContainer();

2. creates the designated wrapper with instantiateReactComponent().

This is what we covered in {post one}.

# Initialize ReactCompositeComponent[T]

The designated data structure:

The step is the same as well:

1) ReactDOMContainerInfo[ins] represents the container DOM element, document.getElementById(‘root’);

2) TopLevelWrapper is instantiated (TopLevelWrapper[ins]) and is set to ReactCompositeComponent[T]._instance alongside the initialization of other properties;

3) Again, mountComponentIntoNode is the cross point of upper and lower half, within which ReactCompositeComponent[T].mountComponent returns a complete DOMLazyTree that can be used by ReactMount._mountImageIntoNode, a method from lower half.

This is what we covered in the first part of {post two}.

Except for some small differences in regard to argument values, the the top level wrapper related operations are exactly the same as what we discussed in previous posts. After those operations complete, the logic processes to the first ramification that is specific to class component.

# ReactCompositeComponent[T].performInitialMount() — create a ReactCompositeComponent from ReactElement[1]

This step strips the wrapper and creates another ReactCompositeComponent instance to reflect App component.

The designated data structure:

The call stack in action:

The process is very similar to the performInitialMount() in {post two}. The only difference here is that based on ReactElement[1].type, _instantiateReactComponent creates a ReactCompositeComponent for the class component (App) instead of a ReactDOMComponent. To put it briefly:

1) it calls _renderValidatedComponent() which in turn calls TopLevelWrapper.render() to extract ReactElement[1]; 2) it instantiates a ReactCompositeComponent with _instantiateReactComponent (we name the object ReactCompositeComponent[ins]); and 3) it calls ReactCompositeComponent[ins].mountComponent (recursively) through ReactReconciler, and moves on to the next step.

# ReactCompositeComponent[ins].mountComponent() — initialize ReactCompositeComponent[ins]

The designated data structure:

The call stack in action:

Same as in ReactCompositeComponent[T].mountComponent() {post two}, the most important task of this step is to instantiate App with ReactCompositeComponent[ins]._currentElement (ReactElement[1]).

The line in the method that does the job is:

in which the constructor of App gets called.

Then (we name it) App[ins] is set to ReactCompositeComponent[ins]._instance and a back-link is also created through ReactInstanceMap.

This is the custom component App instance which responds to your this.setState(…).

Other operations includes: 1) App[ins].props reference ReactElement[1].props; and 2) ReactCompositeComponent[ins]._mountOrder is set to 2 due to the ++ operating on the global variable nextMountID.

It is important to note that App[ins].render() is another App method we define in the beginning. Unlike TopLevelWrapper[ins].render() that returns a concrete ReactElement instance, App[ins].render() relies on React.createElement() at the time when it is invoked. We are going to visit this method soon.

Since this step is very similar to that initializes the ReactCompositeComponent[T] {post two}, we do not further examine the workhorse method (i.e., mountComponent()).

# ReactCompositeComponent[ins].performInitialMount() — create a ReactDOMComponent

We are here again:

Before the a ReactDOMComponent (we know that this is the class that handle DOM operations) can be created, the ReactElements defined within App[ins] needs to be extracted. To do so, App[ins].render() is called by the following line (in _renderValidatedComponent()) {post two}

Then App[ins].render() triggers

## The cascading calls of React.createElement()

To understand how the ReactElement tree is established, let’s first revisit the App.render() implementation:

In this code snippet I also give the call order of createElement()s which follows a very simple principle: arguments should be resolved (with createElement()) from left to right before a function (of createElement()) gets called.

Then we can examine the creation of each ReactElement {post one}.

creates ReactElement[2]:

; and

creates ReactElement[3]:

(Now the two arguments for 3) are resolved.)

; and

creates ReactElement[4]:

; and

creates ReactElement[5]:

(Now the arguments for 5) are resolved.)

; and

creates ReactElement[6]:

Combined together we got the element tree referenced by renderedElement:

## ReactCompositeComponent[ins]._instantiateReactComponent() — Create ReactDOMComponent[6]

The designated data structure:

The element tree created in the last step is used to create the ReactDOMComponent[6] by the following line (within _instantiateReactComponent()) {post two}

Now ReactReconciler.mountComponent() calls the mountComponent() of the ReactDOMComponent[6] and the logic processes to the lower half.

to be continued…

That's it. If you have any suggestions, or you just like the read, , and send me your comments. Thank you for your time and attention. I'll catch you later.