The ReactDOM Package and Its Uses

Irakli Tchigladze Dec 23, 2021
The ReactDOM Package and Its Uses

Every React developer has a unique approach to using external libraries. Some try to limit their use to core essentials, while others use external libraries for almost everything.

In today’s article, we will tackle one of the essential libraries for React, called ReactDOM. This package provides the necessary methods to manage DOM rendering in React applications.

ReactDOM Package in React

In combination with the generic React library, this package is necessary for building dynamic applications in React. It provides a crucial render() function, an essential building block of any class component.

In general, this package provides methods for interacting with DOM, which allow us to manage the application DOM efficiently. Here is the list of the most essential methods included in the ReactDOM package:

  • Render()
  • findDOMNode()
  • unmountComponentAtNode()
  • hydrate()
  • createPortal()

What Are DOM and Virtual DOM?

In essence, DOM (short for document object model) is a representation of a web page as an object. It provides an API to access, read, or mutate the page’s content.

Before the popularity of React, JavaScript developers worked with libraries like jQuery to directly change the DOM. Manual DOM manipulation was inefficient because browsers re-rendered the whole page, even if the changes to the DOM were barely noticeable.

React gained popularity because it provided a Virtual DOM, an arbitrary copy of DOM which tracks the changes and updates the actual DOM all at once.

This approach was much more efficient, so Virtual DOM remains one of the core features of React to this day. ReactDOM library includes essential features for interacting with DOM in React.

Installing ReactDOM in React

In the past, ReactDOM methods were included in the generic package, but now we have to import them separately. First, you must select the app directory and install the package by entering the following in the command line:

npm install react-dom

Then you can import it. If you’re using the modern version of JavaScript, you can do that using the import statement:

import ReactDOM from 'react-dom'

Alternatively, if you only need to import a render method, you can import it like this:

import { render } from 'react-dom'

You can use the following pattern to import any other specific method.

Irakli Tchigladze avatar Irakli Tchigladze avatar

Irakli is a writer who loves computers and helping people solve their technical problems. He lives in Georgia and enjoys spending time with animals.

LinkedIn

Related Article - React ReactDOM