<noscript>You need to enable JavaScript to run this app.</noscript>
<divid="root"></div>
</body>
</html>
In the index.js file, we use reactDOM.render and mount our app component onto the root element.
index.js
importReactfrom'react';
importReactDOMfrom'react-dom';
import'./index.css';
importAppfrom'./App';
importreportWebVitalsfrom'./reportWebVitals';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
reportWebVitals();
In the browser, in the element DOM tree, we can see that every single component in our react application falls under the root element, the div element with id equals to root.
The react portals allow us to break out of this DOM tree; we can render a component in the DOM node that is not under this root element.
Features
Here are some feature of React Portals:
Render children outside of the parent DOM hierarchy.
Support with event bubbling and capturing.
Can be used with any component, not just modals.
Compatible with server processing.
Makes it easier to create accessible UI components.
Can increase performance by removing unnecessary rendering.
Increases the freedom of UI design.
When to use React Portals?
React Portals are used when you need to display a component outside of its parent DOM hierarchy, such as for modal dialogs, popovers, and tooltips. This provides for more flexibility in the UI design and prevents issues with styling or layout. Portals can also improve speed by reducing unnecessary rendering and DOM manipulation, making them a useful tool in any React application.
How to implement portals in react?
Firstly we will add a DOM node that is outside of the root element.
In index.html, right below the root element, we add a new div tag with id equals portal-root.
<noscript>You need to enable JavaScript to run this app.</noscript>
<divid="root"></div>
<divid="portal-root"></div>
</body>
</html>
Secondly, We will create a new Component, namely, PortalDemo.js; within the file, we will create a functional component.
In the JSX, we will add a simple heading tag.
PortalDemo.js
importReactfrom"react";
functionPortalDemo() {
return <h1>Portals in react</h1>;
}
exportdefaultPortalDemo;
Then, we will include the component in the app component.
App.js
importReactfrom"react";
import"./App.css";
importPortalDemofrom"./components/PortalDemo";
functionApp() {
return (
<divclassname="App">
<PortalDemo />
</div>
);
}
exportdefaultApp;
After saving all the files and having a look at the browser looking in the DOM tree, we can see that on inspecting the heading, we see that the heading element falls under the root element and not under the “portal-root” element.
Let us change that,
We will use ReactDOM.createPortal method to insert this component under the portal-root node.
So in the PortalDemo component at the top, we need to import the ReactDOM package,
Then in the render method, instead of simply returning the JSX, we will return ReactDOM.createPortal.
This method takes two arguments; the first one is the JSX you want to render; over here, it is the heading, and the second parameter is the DOM node on which we want to mount the component.
PortalDemo.js
importReactfrom"react";
importReactDOMfrom"react-dom";
functionPortalDemo() {
returnReactDOM.createPortal(
<h1>Portals in react</h1>,
document.getElementById("portal-root")
);
}
exportdefaultPortalDemo;
After saving all the files, we still see the heading “Portals in react.”
When we inspect the element, we see that the heading tag is inside the portal-root DOM node.
So in the react application, even though all the components are children to the app component, and the app component is mounted on the root DOM node, it is possible to break away from that and to mount on any DOM node that you wish to use by the help of React portals.
Note: The first parameter to create a portal can be any element that react can render. It can be numbers, strings, JSX, or even components.
Why do we need portals?
One of the use cases, which is brought up, is having to deal with the parent component CSS when the child component is a model, pop-up, or tooltip.
Example
First, we will see how the code works with the portals.
In the index.html, we have two div tags, one with id “root” and the other “modal-root.”
In index.js, we have references to both elements. We have a Modal component.
This is an example of how you might use React.createPortal. I think
it is a pretty neat API that is yet another awesome escape hatch
that React provides for practical reasons. Sometimes you just need
to render something completely outside the React Tree.
</p>
<buttononClick={this.handleShowMessageClick}>
Show Secret Modal
</button>
{this.state.showModal ? (
<ModalonClose={this.handleCloseModal}>
This is the secret modal message!
</Modal>
) : null}
</div>
</div>
)
}
}
ReactDOM.render(<App />,root)
Output:
Now, we remove the react portal from the Modal component from the index.js file by removing the method ReactDOM.createPortal and its second parameter. Modifying it to as shown below:
We can see that without portals, we can see that the output is distorted, and the model does not work the way we want it.
It is because the CSS of the parent is getting used by the model, which is not desirable. We want to have different styling for the model component that is achieved by the portals in react.
Even though a portal can be anywhere in the DOM tree, it behaves like a normal React child in every other way.
An event fired from inside the portal will propagate to the ancestor into containing react tree, even if those elements are not the ancestors in the DOM tree. This phenomenon is known as event bubbling
Example
Assume the following structure
<divid="app-root"></div>
<divid="modal-root"></div>
A Parent component in the div with id as app-root would catch an uncaught, bubbling event from the sibling node in the div with id as modal-root.
The click event is passed through the child component, which is propagated to the parent component despite being in a different DOM tree. However, they are in the same React tree.
Catching an event bubbling up from a portal in a parent component allows the development of more flexible abstractions that are not inherently reliant on portals. For example, if you render a <Modal /> component, the parent can capture its events regardless of whether it’s implemented using portals.
Portals in React allow you to display a child component outside of its parent DOM hierarchy. This is helpful when creating UI elements such as modal dialogues or popovers that must be placed outside of the parent element.
What is the purpose of React Portals?
The purpose of React Portals is to provide a method to render a child component outside of its parent DOM hierarchy. This enables for more flexible UI design, particularly when creating modal dialogs, popovers, and tooltips.
How do you make a Portal in React?
To make a Portal in React, first create a new container element outside of the root element in your HTML file. Then, using ReactDOM.createPortal(), you can display your component into this new container element.
What is the best use case for using React Portals?
React Portals are best used for displaying UI components that must be placed outside of their parent component, such as modal dialogues or popovers. This can help in avoiding style and layout issues and allowing for more adaptable UI design.
Conclusion
We learned about the concept of portals in react. This is an advanced concept of portals in React that allows us to shift a react component to another DOM node. This article also explained why there is a need for portals in react specifically. We also learned about the implementation of portals in react. You can also consider our React Js Course to give your career an edge over others.