React JS ReactDOM or react-dom is the most widely used package of React. React provides the developers with a package react-dom to access and modify the DOM. Let’s see in brief what is the need to have the package.
What is ReactDOM ?
ReactDOM is a package in React that provides DOM-specific methods that can be used at the top level of a web app to enable an efficient way of managing DOM elements of the web page. ReactDOM provides the developers with an API containing the various methods to manipulate DOM.
How to use ReactDOM ?
To use the ReactDOM in any React web app we must first install the react-dom package in our project. To install the react-dom package use the following command.
npm i react-dom
After installing the package use the following command to import the package in your application file
import ReactDOM from 'react-dom'
After installing react-dom it will appear in the dependenices in package.json file like:
Why ReactDOM is used ?
Earlier, React Developers directly manipulated the DOM elements which resulted in frequent DOM manipulation, and each time an update was made the browser had to recalculate and repaint the whole view according to the particular CSS of the page, which made the total process to consume a lot of time.
To solve this issue, React brought into the scene the virtual DOM. The Virtual DOM can be referred to as a copy of the actual DOM representation that is used to hold the updates made by the user and finally reflect it over to the original Browser DOM at once consuming much lesser time.
Important functions provided by ReactDOM
- render(): This is one of the most important methods of ReactDOM. This function is used to render a single React Component or several Components wrapped together in a Component or a div element.
- findDOMNode(): This function is generally used to get the DOM node where a particular React component was rendered. This method is very less used like the following can be done by adding a ref attribute to each component itself.
- unmountComponentAtNode(): This function is used to unmount or remove the React Component that was rendered to a particular container.
- hydrate(): This method is equivalent to the render() method but is implemented while using server-side rendering.
- createPortal(): It allow us to render a component into a DOM node that resides outside the current DOM hierarchy of the parent component.
Key features of ReactDOM :
- ReactDOM.render() replaces the child of the given container if any. It uses a highly efficient diff algorithm and can modify any subtree of the DOM.
- React findDOMNode() function can only be implemented upon mounted components thus Functional components can not be used in findDOMNode() method.
- ReactDOM uses observables thus provides an efficient way of DOM handling.
- ReactDOM can be used on both the client-side and server-side.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!