difference between dom and react dom

What is the Difference Between DOM and React DOM?

As a web developer, you must have heard about DOM and React DOM while working with JavaScript and React. Although they are related, there are some significant differences between them. In this post, we will explain the difference between DOM and React DOM.

What is DOM?

The DOM (Document Object Model) is a programming interface for web documents. It represents the page so that programs can change the document's structure, style, and content. The DOM represents the document as nodes and objects. That way, programming languages can interact with the page.

The DOM is essentially the way JavaScript interacts with HTML and XML documents. When a web page is loaded, the browser creates a Document Object Model of the page. The HTML DOM model is constructed as a tree of Objects:


<html>
  <head>
    <title>Document Title</title>
  </head>
  <body>
    <h1>Heading 1</h1>
    <p>This is paragraph 1.</p>
    <p>This is paragraph 2.</p>
  </body>
</html>

In the example above, the HTML DOM model is constructed as follows:

  • An HTML document contains only one root element (<html>).
  • The HTML document is represented by the Document object.
  • The Document object contains two child nodes: the Head and Body nodes.
  • The Head node contains one child node: the Title node.
  • The Body node contains two P (Paragraph) nodes and one H1 (Heading) node.

What is React DOM?

React is a JavaScript library for building user interfaces. React DOM is the name of the library responsible for rendering React components in the browser.

React DOM takes care of updating the DOM to match the React components’ virtual DOM. When a component’s state or props change, React DOM updates the virtual DOM, compares it to the old virtual DOM, and then updates the real DOM with only the changes needed. This process is called Reconciliation.

React creates a virtual representation of the UI called the Virtual DOM. The Virtual DOM is an in-memory representation of the real DOM. It’s a lightweight copy of the real DOM, and it only contains properties that are necessary for rendering. The Virtual DOM is a JavaScript object that is easy to manipulate and traverse. It allows React to efficiently update only the parts of the UI that need to be updated.


ReactDOM.render(
  <h1>Hello World</h1>,
  document.getElementById('root')
);

In the example above, we use ReactDOM.render() to render a React component called <h1>Hello World</h1> to an HTML element with an ID of root.

Differences Between DOM and React DOM

The main difference between the DOM and React DOM is how they handle updates. The DOM updates the entire page when new data arrives, while React DOM only updates the necessary parts of the page. The Virtual DOM makes it possible to avoid costly full-page updates.

Another significant difference is that React DOM manipulates the Virtual DOM, while the DOM directly manipulates the page. React DOM takes care of updating the real DOM to match the Virtual DOM, which is a lighter and faster representation of the real DOM. In contrast, the DOM directly manipulates the page and is slower and less efficient.

In summary, the main difference between DOM and React DOM is that DOM directly manipulates the page, while React DOM manipulates the Virtual DOM. React DOM only updates the necessary parts of the page, making it faster and more efficient than DOM.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe