What is DOM in React?


DOM stands for ‘Documents Object Model’. It’s the World Wide Web Consortium's fundamental logical homepage model.

Every time the UI state of an application changes, the DOM is updated to reflect the change. Whenever the application user interface is updated, the DOM is rendered which ultimately impacts the application performance, resulting in slower loading.

The complexity of the DOM and the numerous UI components further depletes the performance because each change necessitates a new rendering of the page.

However, the virtual DOM concept in React has changed everything. Let’s see below −

What is DOM?

To put it another way, DOM is the structural representation of the UI of the web page in a tree form with all its components and associated characteristics as its branches. Any component on a website page can have its data accessed and updated using the DOM's dialect interface.

Every bit of HTML that you create is placed onto the tree like a branch. Using JavaScript, you may reach all of these networks (HTML components) and modify their attributes, styles, and other properties.

This indicates that JavaScript may quickly obtain and alter the website, thanks to the DOM. Therefore, a root node must exist in every tree, correct? The node, in this case, documents. Every Html element you create will become directly or indirectly a descendant of the document for the root node.

What is DOM in React?

React introduces the concept of virtualDOM, which functions as a lightweight replica of the real DOM. Therefore, there is one object in React Virtual DOM for every object that is present in the real DOM. Although it is identical, it is unable to alter the document's design in any way.

DOM manipulation takes time, but virtual DOM manipulation happens quickly because nothing is getting fetched on the screen. As a result, the virtual DOM rather than the real DOM is updated first whenever the state of our application changes, which improves the performance big time.

How Virtual DOM Works in React?

A virtual DOM is formed whenever something new is introduced to the application, and it is visualized as a tree.

A node in this tree represents each component of the application. Therefore, a new Virtual DOM tree is constructed whenever the state of any element changes.

The previous Virtual DOM tree is then compared to the newly created Virtual DOM tree, and the differences are noted. The optimal technique to make these changes to the real DOM is then determined. Only the changed items will now be rendered on the page.

Refreshing the DOM

If you are familiar with JavaScript, you might see users upload the elements of the DOM utilizing the "getElementByClass()" or "getElementById()" methods. The DOM is modified when there's a modification in the app's status to represent the User Interface's changes.

For instance:

JavaScript

// Simple getElementById() method document.getElementById('some−id').innerValue = 'updated value;

These events exist when submitting the code, as mentioned earlier, to a JavaScript file or the console −

  • It eliminates the particular element's child component.

  • The website decodes the HTML

  • and adds the "updated value" to the component (DOM) to locate the component with this identity.

  • Refresh the design.

  • Updates the child and parent nodes' CSS values.

  • Lastly, go around the tree, then draw it upon the browser's window.

As a result, altering the DOM entails more than simply modifying the content.

ReactDOM

To offer an effective approach to controlling DOM components of the web browser, ReactDOM is a library that offers DOM−specific functions which can be employed at the elite tier of the web application.

  • ReactDOM employs measurements and offers an effective method of manipulating the DOM.

  • If there are any descendants of the provided container, ReactDOM.render() substitutes them. It has a very effective diff technique and can change a specific DOM subset.

  • Both the server−side and the client−side can make use of ReactDOM.

  • Functional elements can't be deployed in the findDOMNode() method because the operation is only performed on connected components.

The following techniques and a few others are available in ReactDOM's API for developers.

  • createPortal()

  • hydrate()

  • unmountComponentAtNode()

  • findDOMNode()

  • render()

You need to initially import ReactDOM from the react−dom module employing the below code snippet to be used in a React web application −

import * as ReactDOM from 'react−dom';

ReactDOM.createPortal(child, container)

This procedure requires the two variables listed below.

  • container − This option anticipates the component's rendering container.

  • child − This argument anticipates rendering either a React Element or a JSX expression.

This function doesn't produce anything.

ReactDOM.unmountComponentAtNode

This function needs a React Element in the Window DOM and only accepts a single parameter, the component.

  • Return Type − If the operation is successful, it sends the DOM node in which this component was drawn; if not, it returns empty.

  • unmountComponentAtNode − By using this function, the React Component, which was also displayed to a specific box, can be unmounted or removed. Consider an alert component like an illustration. Deleting the component within a relatively short period is preferable to increase the website's efficiency.

findDOMNode()

The most common application of this function is to locate the DOM node in which a specific React element got rendered. This technique, which is hardly ever used, allows you to accomplish the same by giving each element its reference attribute.

ReactDOM.render

This function accepts a maximal of three parameters, which are listed below.

  • container − This option anticipates the element's rendering container.

  • element − This argument anticipates rendering either a React Element or a JSX expression.

  • callback − This additional parameter anticipates the execution of a function after the render is finished.

  • Return Type − If a rootless component has been rendered, this function will return null. Otherwise, it reverts to an allusion toward the component.

DOM Elements

For cross−browser accessibility and efficiency, React develops a browser−independent DOM framework. You use this chance to smooth down a few of the DOM implementation's jagged boundaries in the window.

All DOM attributes and properties in React, especially event handlers, should be in camelcase. For instance, the React bar Index feature is equivalent to the HTML tab index property.

The data−* and aria−* properties are the only ones that do not require lowercase letters. You might use an area label as an illustration of an area label.

Wrapping Up

In React, whenever we manipulate an element's virtual DOM, we avoid the set of operations necessary that we usually follow in the actual DOM.

Virtual DOM is a technique React employs to enhance application speed. It offers a method of determining exactly what has changed between two render trees and only updates the essential portions of the real DOM.

Updated on: 07-Nov-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements