React component that renders its children inside a portal attached to the document.
import { useRef } from "react";
import DocumentPortal from "react-document-portal";
const Example = (props) => {
const portalRef = useRef(null);
return (
<DocumentPortal as="section" ref={portalRef}>
<p>This content is rendered inside a portal.</p>
</DocumentPortal>
);
};astag name used to create the portal container, defaults to"div".childrenelements to render inside the portal.refoptional React ref that receives the created DOM element.
A React portal, or null before the DOM node exists.
Requires React v16.8.0 or higher.
This component will not break SSR. On the server, the portal will render null until the DOM is available in the browser.