React Portals: Escaping the DOM Hierarchy (2024)

Understanding Portals

In the world of React, components typically render their children within the same DOM hierarchy. While this is often desirable, there are situations where you might want to render a component in a completely different part of the DOM tree. This is where React Portals come into play.

A portal is a way to render child components into a DOM node that exists outside the parent component’s DOM hierarchy. This can be particularly useful for creating elements like modals, tooltips, or overlays that need to appear on top of other content without being constrained by the parent component’s styling.

How Portals Work

React provides the createPortal function to create a portal. It takes two arguments:

  • children: The React element or elements to be rendered.
  • container: The DOM node where the children should be rendered.

The createPortal function returns a new React element that can be rendered like any other component. However, the actual DOM nodes will be rendered into the specified container, outside of the parent component’s DOM tree.

When to Use Portals

  • Modals and Overlays: Portals are ideal for creating modals, tooltips, and other components that need to appear on top of the main content.
  • Rendering Content Outside a Parent Component: If you have a component with overflow: hidden and need to render content outside of it, portals can be used.
  • Customizing Component Rendering: In some cases, you might want to render a component in a specific location based on certain conditions.

Example: Creating a Modal with a Portal

import React, { ReactDOM } from 'react';

const Modal = ({ children }) => {
  const modalRoot = document.getElementById('modal-root');

  return ReactDOM.createPortal(children, modalRoot);
};

const App = () => {
  const [showModal, setShowModal] = useState(false);

  return (
    <div>
      <button onClick={() => setShowModal(true)}>Open Modal</button>
      {showModal && <Modal><div>This is my modal content</div></Modal>}
    </div>
  );
};

In this example, the Modal component uses createPortal to render its children into a DOM element with the id modal-root. This allows the modal to appear on top of other content without being affected by the parent component’s styling.

Important Considerations

  • Accessibility: When using portals for modals, ensure proper focus management and accessibility guidelines are followed.
  • Performance: While portals don’t inherently impact performance, excessive use of portals could potentially affect rendering.
  • DOM Manipulation: Be cautious when manually manipulating the DOM within a portal, as it can lead to unexpected behavior.

By understanding the concept of portals and their appropriate use cases, you can effectively create more complex and interactive user interfaces in your React applications.

For More latest tech news, follow Gadgetsfocus on Facebook,  and TikTok. Also, subscribe to our YouTube channel.

Ibad Ur Rahman
Ibad Ur Rahmanhttps://gadgetsfocus.com
Ibad Ur Rahman is a tech enthusiast and the lead editor at GadgetsFocus. With years of experience diving deep into consumer electronics, Ibad specializes in breaking down complex tech specifications into clear, actionable advice. His rigorous approach to aggregating real-world data and testing insights ensures that readers get the unvarnished truth about the latest smartphones, laptops, and smart home gadgets.

More from author

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related posts

Advertisment

Latest posts

Selling or Trading In Your OnePlus Phone? Why You Need to Act Now

The short answer: The resale and trade-in values for OnePlus phones in the US and UK are dropping rapidly following the brand's exit from...

URGENT: How to Save Your OnePlus Community Posts and Data Before August 16

The short answer: The North American and European OnePlus Community forums will permanently shut down on August 16, 2026. To save your posts, photos,...

Honor “Robot Phone” Showcased: Gimbal Camera and Physical AI Modules (July 2026)

The short answer: Honor has officially showcased its highly anticipated "Robot Phone" today, July 28, 2026. Developed in partnership with Arri, the device features...