Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 1650

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 62k
Author
Asked: November 25, 20242024-11-25T10:07:09+00:00 2024-11-25T10:07:09+00:00

Deep dive into React codebase [EP4: How ReactDOM.render works]

  • 62k

In today's episode, we take a bite-sized approach to learning how ReactDOM.render works and what it does!

Quick recall

In the previous episode we figured out how React.createElement works. In short:

There are two functions:

  • createElement – for production use
  • createElementWithValidation – for dev purposes

createElementWithValidation uses createElement internally, but adds validation and meaningful warnings.
createElement returns a JS object with a bunch of properties. This object is a virtual DOM node.

For us the most important properties are:

  • type – defines type of the element
  • props – contains all props passed to the element
  • children – contains all children of the element

How ReactDOM.render works

Recalling the assumption

In the previous episode, we formed an assumption regarding how react-dom works as a whole. Here it is:

react-dom creates a real DOM structure, based on the virtual DOM, and mounts the result in the provided container.

Continue with testing the assumption

Our entry point is the render function, it takes three arguments.

  • element – React element to be rendered
  • container – DOM element to render in
  • callback – function to be executed after render happens

Inside render function ReactDOM does couple of checks.

isValidContainer

isValidContainer checks whether the container is a suitable node.
This check is based on a couple of comparisons, like this one.

container.nodeType === 1; 
Enter fullscreen mode Exit fullscreen mode

nodeType is an integer, that indicates what the node is.
1 for Element node, 3 for Text node and so on.

To pass this check a container should be either:

  • Element – the most general class of all element objects
  • Document – the main object of the rendered DOM
  • DocumentFragment – lightweight version of Document separated from the rest of the DOM

And on a one special comparison.

node.nodeType === 8 && node.nodeValue === ' react-mount-point-unstable ' 
Enter fullscreen mode Exit fullscreen mode

This check detects whether the provided container is a comment with react-mount-point-unstable text.

<!-- react-mount-point-unstable --> 
Enter fullscreen mode Exit fullscreen mode

It's undocumented and unstable feature, that allows to render React components without extra wrapper, like <div id="root"></div>.

isModernRoot

isModernRoot validates whether the container wasn't previously passed to createRoot function. This check detects:

  • absence of _reactRootContainer property. This property indicates, that element was passed to the render function.
  • presence of __reactContainer$qk4eqywqqse property. This property indicates, that element was passed to the createRoot function. $qk4eqywqqse – is a unique hash, generated on every new ReactDOM instance

createRoot is a new API introduced in React 18. It fixes the issue of passing the container every time we want to explicitly render.

Old API

const container = document.querySelector('#root');  // Initial render. Container is explicitly accessed. ReactDOM.render(<App text="Hello" />, container);  // Subsequent renders. Container is explicitly accessed. ReactDOM.render(<App text="Hello world!" />, container); 
Enter fullscreen mode Exit fullscreen mode

New API

// First, we create a root const root = ReactDOM.createRoot(document.querySelector('#root'));  // Initial render. Container is implicitly accessed. root.render(<App name="Hello" />);  // Subsequent renders. Container is implicitly accessed. root.render(<App name="Hello world!" />); 
Enter fullscreen mode Exit fullscreen mode

legacyRenderSubtreeIntoContainer

After these two checks render passes element, container and callback to the legacyRenderSubtreeIntoContainer function and returns the result.

We'll examine what this function does and how it works in the next episode!

Wrap up

Today we learned

Today we learned, that ReactDOM.render function is basically a wrapper for legacyRenderSubtreeIntoContainer. But at the same time it checks a couple of crucial things:

  • Is passed container a suitable element?
  • Was the container previously passed to ReactDOM.createRoot function?

Follow me on Twitter for daily bite-sized content like this! Let's become better engineers together!

javascriptprogrammingreactwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.