For AI agents: the complete documentation index is available at /next/zh/llms.txt, the full documentation bundle is available at /next/zh/llms-full.txt, and this page is available as Markdown at /next/zh/api/react/Function.createPortal.md.
  • 简体中文
  • @lynx-js/react / createPortal

    函数: createPortal()

    function createPortal(vnode: ComponentChild, container: NodesRef): ReactNode

    vnode 渲染到 container 中,而不是当前组件在元素树中的位置,同时让它 仍然保留在 React 树中。

    container 是一个 NodesRef,可以通过回调 reflynx.createSelectorQuery().select(...) 获取。

    可用性
    • @lynx-js/react 0.121.0 起提供。
    • 自 0.123.3 起,声明的返回类型为 ReactNode;在 0.123.2 及更早版本中为 Preact 的 VNode<any> | null,二者没有运行时差异。

    参数

    范围类型描述
    vnodeComponentChild要渲染到 container 中的 React 节点。
    containerNodesRef作为渲染目标的 NodesRef

    返回

    ReactNode

    一个 ReactNode 占位节点,需要包含在调用处的 JSX 中;实际渲染输出会进入 container

    备注

    Context 仍然会跨越 portal 边界传递,portal 内的状态更新行为与其他组件一致。 以下行为与 react-dom 不同:

    • 事件不会沿 React 树冒泡。ReactLynx 沿用了 Preact 的实现方式,没有合成 事件系统,因此事件遵循页面实际的元素结构。
    • portal 中的子树只在后台线程渲染,不参与主线程首屏渲染。
    • 不支持跨页面或跨原生容器挂载。

    定义于

    @lynx-js/react/runtime/lib/snapshot/lynx/portals.d.ts:31

    示例

    除非另有说明,本项目采用知识共享署名 4.0 国际许可协议进行许可,代码示例采用 Apache License 2.0 许可协议进行许可。