Open In App

How to use portals for rendering content outside the parent DOM hierarchy?

Modern web development requires the ability to render content outside of the parent DOM hierarchy. Portals come in handy in such scenarios. They provide a seamless way to render UI elements across different parts of a web application, improving flexibility and performance.

We will discuss about the following usage of portals that render outside the parentDOM hierarchy.



Understanding React Portals:

Using Portals in React: Step-by-Step Guide:

Parameters:

import {createPortal} from 'react-dom';
createPortal(children, domNode);

Benefits of using React Portals:

Creating a Modal without using Portals :

When creating a modal without portals, a simple component is designed to toggle its visibility using local state management. This method involves rendering the modal content within the component’s DOM hierarchy, potentially causing issues with z-index stacking and CSS containment.



Below is an example that illustrate’s how to creating a modal without using portals.




import { useState } from "react";
import "./App.css";
 
const Modal = ({ onClose }) => {
    return (<div className="modal">It's Modal
        <button onClick={onClose}>Ok</button>
    </div>
    );
}
 
function App() {
    const [showModal, setShowModal] = useState(false);
    const onClose = () => setShowModal((prev) => !prev);
 
    return (
        <div className="wrapper">
            <button onClick={onClose}
                className="btn">
                Show Modal using portal
            </button>
            {showModal && <Modal onClose={onClose} />}
        </div>
    );
}
 
export default App;




/* App.css */
 
.wrapper {
  width: 300px;
  padding: 20px;
  display: flex;
  justify-content: center;
  flex-direction: column;
  align-items: center;
  height: auto;
  overflow: hidden;
  gap: 20px;
  position: relative;
  border: 2px solid black;
}
 
.modal {
  background-color: green;
  width: 200px;
  height: 50px;
  display: flex;
  justify-content: space-evenly;
  align-items: center;
  border-radius: 15px;
  position: absolute;
  left: 50%;
  top: 20%;
  transform: translate(-50%, -20%);
}
 
.btn {
  border: 2px solid black;
  width: 200px;
  height: 50px;
  border-radius: 15px;
}

Output:portals

Modal without using Portal

Creating a Modal using Portals :

In contrast, utilizing portals for modal creation involves rendering the modal content outside the parent DOM hierarchy. This ensures proper layering and containment, mitigating potential styling conflicts. With portals, modal elements are seamlessly inserted into a specified DOM node, maintaining cleaner and more predictable rendering behavior.

Example: Below is an example that illustrate’s how to creating a modal using portals.




import { useState } from 'react'
import { createPortal }from 'react-dom'
import './App.css'
 
const Modal = ({ onClose }) => {
    return (
        <div className="modal">
            It's Modal
            <button onClick={onClose}>Ok</button>
        </div>
    )
}
 
function App() {
    const [showModal, setShowModal] = useState(false)
    const onClose = () => setShowModal((prev) => !prev)
 
    return (
        <div className="wrapper">
            <button onClick={onClose}
                className="btn">
                Show Modal using portal
            </button>
            {
                showModal &&
                createPortal(
                    <Modal onClose={onClose} />,
                    document.body)
            }
        </div>
    )
}
 
export default App




/* App.css */
.wrapper {
  width: 300px;
  padding: 20px;
  display: flex;
  justify-content: center;
  flex-direction: column;
  align-items: center;
  height: auto;
  overflow: hidden;
  gap: 20px;
  position: relative;
  border: 2px solid black;
}
 
.modal {
  background-color: green;
  width: 200px;
  height: 50px;
  display: flex;
  justify-content: space-evenly;
  align-items: center;
  border-radius: 15px;
  position: absolute;
  left: 50%;
  top: 20%;
  transform: translate(-50%, -20%);
}
 
.btn {
  border: 2px solid black;
  width: 200px;
  height: 50px;
  border-radius: 15px;
}

Output:

Modal using portal


Article Tags :