Usage
Portal is a wrapper component for the ReactDOM.createPortal API.
Render any component or element at the end of document.body or at a given element. The Modal and Drawer components are wrapped in Portal by default.
Use Portal to render a component or an element at a different place (defaults to the end of document.body).
Portal is useful when you want to prevent parent styles from interfering with children.
Usually all these styles are related to position and z-index properties,
and portals are used for components with fixed position, for example, modals.
In the example above, the div element is rendered outside of the parent main (before the closing body tag). The element will not be affected by the parent z-index.
Reuse target node
By default, Portal reuses the same target node for all instances (reuseTargetNode={true}).
className, style and id props are not applied to the shared node.
To create a new target node for each instance, set reuseTargetNode={false}. In the following
example, all three paragraphs will be rendered in separate target nodes:
Specify target DOM node
You can specify the DOM node where the portal will be rendered by passing the target prop:
Alternatively, you can specify a selector to render the portal in an existing element:
If the selector does not match any element, the portal content is not rendered.
Server side rendering
createPortal is not supported during server-side rendering.
All components inside Portal are rendered only after the application was mounted to the DOM.
OptionalPortal component
The OptionalPortal component lets you configure whether children should be rendered in Portal.
It accepts the same props as the Portal component: