> 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.

# Function: forwardRef()

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

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

## Type Parameters

| Type Parameter | Default type | Description         |
| -------------- | ------------ | ------------------- |
| `T`            | -            | DOM 节点的类型。          |
| `P`            | `object`     | 组件接受的 props（如果有的话）。 |

## Parameters

| Parameter | Type                                                      | Description                                                                                  |
| --------- | --------------------------------------------------------- | -------------------------------------------------------------------------------------------- |
| `render`  | `ForwardRefRenderFunction`\<`T`, `PropsWithoutRef`\<`P`>> | 参见 [ForwardRefRenderFunction](https://react.dev/reference/react/forwardRef#render-function)。 |

## Returns

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

## See

- [React Docs](https://react.dev/reference/react/forwardRef)
- [React TypeScript Cheatsheet](https://react-typescript-cheatsheet.netlify.app/docs/basic/getting-started/forward_and_create_ref/)

## Example

```tsx
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>
));
```
