react / forwardRef

Function: forwardRef()

function forwardRef<T, P>(render: ForwardRefRenderFunction<T, PropsWithoutRef<P>>): ForwardRefExoticComponent<PropsWithoutRef<P> & RefAttributes<T>>

Lets your component expose an element to a parent component using a ref.

Type Parameters

Type Parameter Default type Description
T - The type of the element node.
P object The props the component accepts, if any.

Parameters

Parameter Type Description
render ForwardRefRenderFunction<T, PropsWithoutRef<P>> See the ForwardRefRenderFunction.

Returns

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

See

Example

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

Defined in

.pnpm/@types+react@18.3.11/node_modules/@types/react/ts5.0/index.d.ts:1606

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