React component that renders its children inside a portal attached to the document.
npm install react-document-portalReact component that renders its children inside a portal attached to the document.
- :gift: Lightweight !npm bundle size
- :smile: Easy to use with simple API
- :printer: Server-side rendering out of the box
``jsx
import { useRef } from "react";
import DocumentPortal from "react-document-portal";
const Example = (props) => {
const portalRef = useRef(null);
return ( This content is rendered inside a portal.
);
};
`
- as tag name used to create the portal container, defaults to "div".children
- elements to render inside the portal.ref
- optional 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.