Modern web applications need interfaces that feature elements like modals and tooltips. But these UI components might not always integrate well with the existing component structure.

React offers a solution to this problem through a feature called Portals.

How react portals and render UI elements outside the element’s hierarchy

What Are React Portals?

React Portals provide a way to render a component’s content outside its parent component hierarchy. In other words, they allow you to render a component’s output into a different DOM element that is not a child of the current component.

This feature is handy when dealing with UI components that need to render at a higher level in the DOM, such as modals or overlays.

React app folder structure after initialization

Uses for React Portals

React Portals come in handy in various scenarios:

How React Portals Work

Traditionally, when you render a component in React, you attach its output to the parent component’s DOM node. This works well for most scenarios, but it becomes limiting when you need to render content outside the parent’s hierarchy.

Suppose you’re creating a modal dialogue. By default, you’ll place the modal’s content within theparent component’s DOM node.

This can lead to styling conflicts, and other unintended behavior since the modal’s content inherits the styles and constraints of its parent components.

React Portals address this limitation by letting you specify a target DOM element where a component’s output should render.

This means you may render any UI element outside the parent’s DOM hierarchy, breaking free from the constraints of the parent’s styles and layout.

How to Use React Portals

Modals are a perfect example of when you might use React Portals. This sample code explains the procedure.

Set Up a Basic React Application

Before creating a portal, ensure you have a basic React application set up. You can usetools like Create React Appto quickly scaffold a project.

Create a Portal for Modals

To create a portal, use theReactDOM.createPortal()function. It takes two arguments: the content you want to render, and a reference to the DOM node to render it in.

In this example, the Modal component renders its children using a portal. The content will appear in the DOM element with the ID ‘root’.

You can define the contents of a specific Modal in a specific component. For example, this ModalContent component contains a paragraph and aClosebutton:

You can then open the Modal via a button click defined in App.js:

In this example, the Modal component renders its content using a portal, detached from the main component hierarchy.

Real-World Examples

React Portals are useful in various everyday situations.

Best Practices for Portal Use

To get the most out of React Portals, follow these tips:

Benefits of React Portals

React Portals offer several benefits that can enhance the development of complex user interfaces. They:

Potential Pitfalls and Considerations

While React Portals are helpful, keep these things in mind:

Propelling UI Beyond the Norm

React Portals offer a strong solution for handling intricate UI situations that need content to appear beyond the limits of parent components.

By grasping portals and using best practices, you can build user interfaces that are more adaptable and easier to maintain, all while avoiding issues.

Whether you’re crafting modals or bringing in third-party libraries, React Portals provide a powerful answer for addressing demanding UI needs.