React Portals: An Introduction

Benjamin Franklin
3 min readMar 1, 2023

--

React is a popular JavaScript library used for building user interfaces. It allows developers to build complex and interactive UI components using a declarative syntax. One of the core features of React is its ability to manage the state of the application and efficiently update the UI when the state changes. However, there are some use cases where it can be challenging to manage the UI state with React alone, such as rendering modal windows, popups, or tooltips.

This is where React portals come in. React portals provide a way to render a component’s subtree in a different part of the DOM hierarchy, outside of its parent component. In simpler terms, it allows you to render a component outside of its parent component’s DOM hierarchy. This can be particularly useful when you need to render a component in a different part of the DOM, such as the document body, to ensure it remains visible regardless of its parent’s positioning.

How React Portals Work

React portals use a technique called “portal rendering”. The portal rendering technique involves creating a container element outside of the parent component and rendering the component inside that container element.

Here’s an example of how React portals work in practice:

import React from ‘react’;
import ReactDOM from ‘react-dom’;

const MyComponent = () => {
return ReactDOM.createPortal(
<div>My component is rendered outside of its parent component!</div>
document.body
);
}

In the above example, we’ve used the ReactDOM.createPortal method to render the div element outside of its parent component. The first argument to the createPortal method is the element that we want to render, and the second argument is the DOM node where we want to render the element. In this case, we’ve specified document.body, which means that the div element will be rendered outside of the parent component and directly in the body of the HTML document.

Benefits of Using React Portals

  1. Improved Performance: React portals can improve performance by reducing the amount of DOM manipulation required by React. Since the portal’s subtree is rendered outside of the parent component, React doesn’t need to update the parent component’s DOM tree every time the portal updates.
  2. Better Separation of Concerns: React portals allow developers to separate concerns between components. By rendering components outside of their parent’s DOM hierarchy, it’s easier to manage the state and lifecycle of each component independently.
  3. Improved Accessibility: React portals can improve accessibility by ensuring that modals and other elements are rendered in a predictable and consistent way. By rendering elements outside of the parent component’s hierarchy, it’s easier to ensure that they’re properly styled and accessible.
  4. Easier to Manage Z-Index: React portals make it easier to manage the z-index of elements in the UI. Since the portal’s subtree is rendered outside of the parent component, it’s easier to ensure that elements are properly layered and positioned in the UI.

Conclusion

React portals are a powerful feature that allows developers to render components outside of their parent’s DOM hierarchy. This can be particularly useful when building modals, popups, and other UI elements that need to be rendered in a different part of the HTML document. By using React portals, developers can improve the performance, accessibility, and maintainability of their React applications.

--

--

Benjamin Franklin
Benjamin Franklin

Written by Benjamin Franklin

Enthusiastic software engineer & seasoned traveller who likes to dabble in a lot of different things.

No responses yet