> For AI agents: the complete documentation index is available at /next/llms.txt, the full documentation bundle is available at /next/llms-full.txt.

# Function: createPortal()

> **createPortal**(`vnode`: `ComponentChild`, `container`: `NodesRef`): `ReactNode`

Renders `vnode` into `container` instead of the current component's position
in the element tree, while keeping it in the React tree.

`container` is a `NodesRef`, obtained either from a callback `ref` or from
`lynx.createSelectorQuery().select(...)`.

## Parameters

| Parameter   | Type             | Description                                |
| ----------- | ---------------- | ------------------------------------------ |
| `vnode`     | `ComponentChild` | The React node to render into `container`. |
| `container` | `NodesRef`       | The `NodesRef` target to render into.      |

## Returns

`ReactNode`

A `ReactNode` placeholder to include in the JSX at the call site;
the rendered output goes into `container`.

## Remarks

Context still flows across the portal boundary, and state updates inside the
portal behave like they do in any other component. A few behaviors differ
from `react-dom`:

- Events do not bubble along the React tree. ReactLynx follows Preact's
  approach, which has no synthetic event system, so events follow the actual
  element structure of the page.
- The portaled subtree renders on the background thread only, and does not
  participate in main-thread first-screen rendering.
- Mounting across pages or across native containers is not supported.

## Example

**This is an example below: react-apis**

**Entry:** `src/create-portal`
**Bundle:** `dist/create-portal.lynx.bundle` | Web: `dist/create-portal.web.bundle`

```tsx
import { createPortal, type ReactNode, root, useState } from "@lynx-js/react";
import type { NodesRef } from "@lynx-js/types";

import "./index.css";

function CreatePortalExample() {
  const [portalHost, setPortalHost] = useState<NodesRef | null>(null);
  const [visible, setVisible] = useState(true);

  const togglePortal = () => {
    setVisible((current) => !current);
  };

  return (
    <page>
      <view className="App">
        <text className="Title">createPortal</text>

        <view className="Section">
          <text className="Label">Source</text>
          <view className="Button" bindtap={togglePortal}>
            <text className="ButtonText">{visible ? "Hide portal" : "Show portal"}</text>
          </view>
          {portalHost && visible
            ? (createPortal(
              <view className="PortalContent">
                <text className="PortalText">Rendered with createPortal</text>
              </view>,
              portalHost,
            ) as unknown as ReactNode)
            : null}
        </view>

        <view className="Section">
          <text className="Label">Target</text>
          <view className="PortalHost" ref={setPortalHost} />
        </view>
      </view>
    </page>
  );
}

root.render(<CreatePortalExample />);

```

v0.2.0


