Skip to content
ClapelloNotes by Barath
ReactCh 16 · 6/6
Core4 min readUpdated

React / 93

Portals and Activity

TL;DR

createPortal renders a modal somewhere else in the DOM while it stays in your React tree. Activity hides UI without unmounting it, so its state survives the switch.

createPortal: render somewhere else

createPortal(children, domNode) from react-dom renders children into another DOM node, usually document.body, that must already exist.

ConfirmButton.jsx
import { useState } from "react";
import { createPortal } from "react-dom";
 
export default function ConfirmButton() {
  const [open, setOpen] = useState(false);
  return (
    <div className="card">
      <button onClick={() => setOpen(true)}>Delete order</button>
      {open && createPortal(
        <div className="modal" role="dialog" aria-modal="true">
          <p>Delete this order?</p>
          <button onClick={() => setOpen(false)}>Cancel</button>
        </div>,
        document.body
      )}
    </div>
  );
}

React tree

decides props, context and event bubbling

<App>
<Card onClick>
<Modal> (portal)

DOM tree

decides layout, clipping and stacking

<body>
<div id="root">
<div class="card">
<div class="modal">
A portal moves the DOM nodes, not the component. In the React tree the modal is still a child of the card, so context flows down and events bubble up through the card.

That's the key idea: only the DOM position changes. Context from above still reaches the modal, and a click inside it bubbles to the card's onClick, even though the DOM nodes aren't nested.

Escape the overflow-hidden box

Try it

clicks the card's onClick heard

0

card · overflow: hidden · height 160px

The inline popup is clipped by the card. The portal one covers the page. Click inside the portal and watch the card's counter: events bubble through the React tree.

Portals are also the usual answer for tooltips, toasts and dropdowns that must float above everything. When you build a modal, handle accessibility too: role="dialog", move focus into it, close on Escape and return focus when it closes.

Activity: hide without unmounting

<Activity> (stable since React 19.2) takes a mode of "visible" or "hidden":

Tabs.jsx
import { Activity, useState } from "react";
 
export default function Tabs() {
  const [tab, setTab] = useState("menu");
  return (
    <>
      <TabButtons tab={tab} onChange={setTab} />
      <Activity mode={tab === "menu" ? "visible" : "hidden"}>
        <Menu />
      </Activity>
      <Activity mode={tab === "cart" ? "visible" : "hidden"}>
        <CartNote />
      </Activity>
    </>
  );
}

When mode="hidden", React:

  1. Hides the DOM

    It sets display: none on the children. The DOM nodes stay in the page.
  2. Keeps the state

    Typed text, picked filters, open accordions: all there when you come back.
  3. Cleans up the effects

    Subscriptions and timers stop, exactly like an unmount. They set up again when the content becomes visible.
  4. Updates it at low priority

    Hidden children still re-render on new props, but only after the visible UI is done.

Conditional render

{show && <Cart />}
state
lost
effects
cleaned up
DOM
removed

CSS only

<Cart className="hidden" />
state
kept
effects
keep running
DOM
kept, hidden

Activity

<Activity mode="hidden">
state
kept
effects
cleaned up, re-created on show
DOM
kept, display: none
Activity is the middle ground: the UI keeps its state and DOM like CSS hiding, but its effects stop like an unmount. Updates to hidden content run at low priority.

Hide a tab without losing it

Try it

NoteTab's effect

not mounted
Type a note, switch to Menu, then back. With conditional rendering the note is gone. With Activity it's still there, and its effect stops while hidden.

Because hidden content still renders in the background, you can also use mode="hidden" to pre-render a tab the user is likely to open next. Data read with use(promise) loads during that pre-render. Data fetched in an effect doesn't, because effects don't run while hidden.

Gotchas

Recall

Can you answer these?

0/4

Answer out loud (or in your head) before you tap. That's what makes it stick.

Saved on this device only. No login.