When to Use
- Render modals and dialogs outside the main app container
- Create tooltips and popovers that break out of overflow containers
- Implement dropdown menus with proper z-index stacking
- Render content to specific DOM nodes or selectors
- Avoid CSS overflow and z-index issues
Basic Usage
Component API
string | HTMLElement | RefObject<HTMLElement> | null
required
Target destination for rendering. Can be:
- CSS selector string (e.g.,
"body","#modal-root",".container") - HTMLElement instance
- React ref object
- Valid HTML tag name (e.g.,
"div","main")
React.ReactNode
required
Content to render in the portal
InsertPosition
Where to insert the portal relative to the target:
"beforebegin": Before the target element"afterbegin": First child of target"beforeend": Last child of target"afterend": After the target element
Examples
Modal Dialog
Tooltip System
Dropdown Menu
Notification System
Insert Position Control
Using Ref Object
Nested Portals
Comparison to Native Patterns
Without Portal
With Teleport
Common Use Cases
Full-Screen Overlay
Context Menu
Side Panel
Teleport uses React’s
createPortal under the hood and automatically handles client-side rendering with ClientGate to prevent SSR hydration issues.When using
insertPosition, a temporary wrapper div is created and then replaced with its children after rendering, maintaining a clean DOM structure.