A use case example for React Portals

šŸ“– Neural networks for Javascript developers

The Neural Networks for JavaScript developers book is almost ready! Learn the basics of AI with TensorFlowJs examples. Join now the presale and get a 15$ coupon off the launching price!

We want to use React Portals so that a component is rendered in some other place it was supposed to be rendered. Imagine something like a cut-paste.

So let's say we have this markup:

<body>
     <div id="root"></div>
    <div id="popup-container"></div>
</body>

The id="root" serves as the placeholder for the following app component:

export default function App() {
    return(<>
        <Popup>
            Popup content here
        </Popup>
        <!-- more page content here -->
    </>
}

We want to move the just <Popup> component from within id="root" to id="popup-container".

Why would we want to do this?

Take a look at this codepen.

It just has a popup component that renders its children's elements when a button is clicked.

However, when we open the popup we will see the below problem:
example of a display problem fixed by React Portals

This happens because of how z-index works. It creates stacking contexts, so even if the .popup has z-index: 1000 it cannot escape the context of its parent #root where z-index: 1.

Here is where the React Portals come into play. Even if the <Popup> component is defined in <App>, and the <App> component is rendered in the <div id="root"> we can cut and paste the popup so that is rendered in another parent.

In this case, we will move the popup to <div id="popup-container"> so it can escape the stacking context with z-index: 1.

So, we will first need to import createPortal():

import { createPortal } from "react-dom"

And next move the popup to a given new parent:

const Popup = ({ open, children, onClose }) => {
  return open && createPortal(
    <>
      <!-- popup content here -->
    </>,
    document.getElementById('popup-container')
  )
}

And that's it! This will solve our z-index problems:
react popup problem fixed

You can checkout here the full codepen with the working React Portals example.

You may also be interested in how React Portals handles event delegation.

šŸ“– Neural networks for Javascript developers

The Neural Networks for JavaScript developers book is almost ready! Learn the basics of AI with TensorFlowJs examples. Join now the presale and get a 15$ coupon off the launching price!


Leave a Reply

Your email address will not be published. Required fields are marked *

Home Screencasts Best of Newsletter Search X

Neural Networks for JavaScript developers
Presale - 15$ free coupon

Hi friend! Before you go, just wanted to let you know that in March 2023 I will be launching the TensorFlow.Js by Example course.

This course will include basic concepts of AI and Neural Networks done with TensorFlowJs such as:

  • Working with datasets
  • Visualizing training
  • Deep Neural Networks in JavaScript
  • Reinforcement Learning
  • Convolutional neural networks
  • and much more ...

Also, there will be a lot of examples as:

  • Object detection
  • Natural language processing
  • Face and voice recognition
  • Gaming AI

Join now the waiting list and get a 15$ coupon off the launching price!

X