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/Functions/createPortal.md.
  • 简体中文
  • 函数: createPortal()

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

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

    container 是一个 NodesRef,可以通过回调 ref 或 lynx.createSelectorQuery().select(...) 获得。

    参数

    参数类型描述
    vnodeComponentChild要渲染到 container 中的 React 节点。
    containerNodesRef渲染目标 NodesRef。

    返回

    ReactNode

    需要在调用处放入 JSX 中的 ReactNode 占位符;渲染结果会输出到 container 中。

    备注

    Context 仍会跨越 portal 边界传递,portal 内的状态更新也与其他组件中的表现一致。但有几点行为与 react-dom 不同:

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

    示例

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