React Portals Modal with Example

How to Use React Portals Modal with Example

We all need to How to Use React Portals Modal with Example at some point in most of our projects. We can import createPortal from react-dom to create a modal. Portals provide a way to provide children with a non-original DOM location available without parental DOM leadership.

Event Bubbling Through Portals

Although the site can be anywhere on the DOM tree, it behaves like a normal React child in every other way. Features like context work the same regardless of the child’s site, as the site still exists in the React tree regardless of the position in the DOM tree.

Read Also :  React API Call Using Fetch

This includes event breakdowns. The event cast inside the hole will spread the ancestors to the tree that contains the React, even if those items are not the ancestors in the DOM tree. Downloading the following HTML format:

    <div id="app-root"></div>
    <div id="modal-root"></div>

How to Use React Portals Modal with Example


<!DOCTYPE html>
<html lang="en">
    <meta charset="utf-8" />
    <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="theme-color" content="#000000" />
      content="Web site created using create-react-app"

    <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
    <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
    <link rel="preconnect" href="">
    <link rel="preconnect" href=""
    <link href=",wght@1,600&family=Lobster&display=swap" rel="stylesheet">
    <!-- Boostrap -->



    <script src="" crossorigin></script>


    <title>React App</title>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
    <div id="root-app"></div>


import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'

class Demo extends React.Component {    
    render () {    
        return (    
                <h3>Index Component</h3>
                <App />  

ReactDOM.render(<Demo />,document.getElementById("root"));


import React, { Component } from 'react'
import ReactDOM from 'react-dom'

export default class App extends Component {
    render() {
        return ReactDOM.createPortal (
            <h3>App Component</h3>,document.getElementById("root-app")