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

    forwardRef<T, P = { }>(render: ForwardRefRenderFunction<T, PropsWithoutRef<P>>): ForwardRefExoticComponent<PropsWithoutRef<P> & RefAttributes<T>>

    让你的组件通过 ref 向父组件 暴露一个 DOM 节点。

    类型参数

    类型参数默认类型描述
    T-DOM 节点的类型。
    Pobject组件接受的 props(如果有的话)。

    参数

    参数类型描述
    renderForwardRefRenderFunction<T, PropsWithoutRef<P>>参见 ForwardRefRenderFunction。

    返回

    ForwardRefExoticComponent<PropsWithoutRef<P> & RefAttributes<T>>

    参阅

    示例

    interface Props {
      children?: ReactNode;
      type: "submit" | "button";
    }
    
    export const FancyButton = forwardRef<HTMLButtonElement, Props>((props, ref) => (
      <button ref={ref} className="MyClassName" type={props.type}>
        {props.children}
      </button>
    ));
    除非另有说明,本项目采用知识共享署名 4.0 国际许可协议进行许可,代码示例采用 Apache License 2.0 许可协议进行许可。