# Portals

> How to render a component's HTML outside of its parent DOM node to fix layout constraints like overflow or z-index.

Subject: React | Lesson 25 of 48 | 3 min read
Interactive lesson: https://codewithanimation.in/react/portals
Previous: [Error Boundaries](https://codewithanimation.in/react/error-boundaries.md) | Next: [Client vs. Server State](https://codewithanimation.in/react/client-vs-server-state.md)

## What is a Portal?

Normally, when a React component renders HTML, that HTML is placed directly inside the parent component's HTML in the DOM.

`createPortal` allows you to break this rule. It lets a component stay exactly where it is in the *React Tree*, but 'teleport' its actual HTML to a completely different location in the *DOM Tree* (like attaching it directly to the `<body>` tag).

## The Overflow Problem

Imagine you build a `Tooltip` component inside a `Card`. If the `Card` has `overflow: hidden` in its CSS, your tooltip will get cut off at the edge of the card.

Similarly, Modals often struggle with `z-index` stacking contexts if they are deeply nested inside other components.

Portals let you choose a DOM target outside the original clipping or stacking ancestor. The target's own DOM ancestry and CSS still matter, and a portal does not automatically provide accessible modal behavior.

## How to use createPortal

First, import it from `react-dom` (not `react`!): `import { createPortal } from 'react-dom';`

Once a browser DOM target exists, include the portal in your JSX: `createPortal(<div>My note</div>, targetNode)`. In a server-rendered application, obtain the DOM target after mounting rather than reading `document` during the server render.

The first argument is your JSX, and the second argument is the actual DOM node where you want it to appear.

## Common mistake: Event Bubbling Still Works

The most confusing (and powerful) part of Portals is that they only change the *DOM structure*, not the *React structure*.

Even though a Modal's HTML is teleported to `document.body`, it is still a child of its parent component in React.

This means if someone clicks inside the Modal, the `onClick` event will still bubble up through the React tree to the parent component, exactly as if the portal didn't exist.

## Tip: Context flows through Portals

Because the component remains in the same position in the React tree, any Context Providers wrapping the parent component will still perfectly provide data to the portaled component.

## Try it: Move DOM, keep React ownership

Move a real note outside a clipped card, change its Context value and send an event through its unchanged React parent.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/react/portals

## Check yourself

### Question 1

What is the primary use case for React Portals?

- A. To pass props deeply without drilling them.
- B. To improve rendering performance for large lists.
- C. To render elements like Modals or Tooltips outside of their parent's DOM hierarchy to avoid CSS overflow/z-index issues.

Correct answer: C

Portals allow you to escape restrictive CSS properties on parent containers.

- Why not A: That is the use case for Context, not Portals.
- Why not B: That is virtualization, not Portals.

### Question 2

If a component uses a Portal to render its HTML into `document.body`, what happens to click events triggered inside the portal?

- A. They bubble up through the React tree to the parent component, just like normal.
- B. They bubble up the DOM tree to `document.body`, completely bypassing the React parent.
- C. Events are disabled inside Portals.

Correct answer: A

Portals only change where the HTML is physically placed. They do not change the component's logical position in the React tree.

- Why not B: React's synthetic event system maintains the React tree hierarchy, not the DOM hierarchy.
- Why not C: Events work perfectly inside Portals.

### Question 3

Which package provides the `createPortal` function?

- A. `react`
- B. `react-dom`
- C. `react-router`

Correct answer: B

Because Portals explicitly manipulate actual DOM nodes, the function lives in the `react-dom` package.

- Why not A: Portals are specific to the DOM environment, so they are not in the core React package.
- Why not C: Routing is unrelated to Portals.

## Remember this

- Portals teleport a component's HTML to a different DOM node.
- They are used to escape `overflow: hidden` and `z-index` issues.
- The component's logical position in the React tree does NOT change.
- Events and Context still flow perfectly through the React tree.
