React 中 ForwardRefExoticComponent 和 ForwardRefRenderFunction 有什么区别?

Ngo*_*Lam 10 javascript typescript reactjs react-forwardref

我正在编写一个 React 组件,它可以将 ref 转发给它的孩子

我发现对于函数组件的返回类型,我可以使用ForwardRefExoticComponentForwardRefRenderFunction。但我不确定它们之间有什么区别。

到目前为止,当使用ForwardRefExoticComponent 时,我可以扩展它而ForwardRefRenderFunction不能?我在这里发布了一个与我的案例相关的问题:How to export forwardRef with ForwardRefRenderFunction

如果有人知道他们之间的区别以及他们所做的事情,请帮助我。因为 React 团队似乎没有关于它们的文档(但它们在 react 包中)

Pet*_*rdt 10

ForwardRefExoticComponent

这里采取的定义是

interface ExoticComponent<P = {}> {
    /**
     * **NOTE**: Exotic components are not callable.
     */
    (props: P): (ReactElement|null);
    readonly $$typeof: symbol;
}

interface NamedExoticComponent<P = {}> extends ExoticComponent<P> {
    displayName?: string;
}

interface ForwardRefExoticComponent<P> extends NamedExoticComponent<P> {
    defaultProps?: Partial<P>;
    propTypes?: WeakValidationMap<P>;
}
Run Code Online (Sandbox Code Playgroud)

如果你写出来,你会得到

interface ForwardRefExoticComponent<P> {
    /**
     * **NOTE**: Exotic components are not callable.
     */
    (props: P): (ReactElement|null);
    readonly $$typeof: symbol;
    displayName?: string;
    defaultProps?: Partial<P>;
    propTypes?: WeakValidationMap<P>;
}
Run Code Online (Sandbox Code Playgroud)

前向引用渲染函数

这里采取的定义是

interface ForwardRefRenderFunction<T, P = {}> {
    (props: PropsWithChildren<P>, ref: ((instance: T | null) => void) | MutableRefObject<T | null> | null): ReactElement | null;
    displayName?: string;
    // explicit rejected with `never` required due to
    // https://github.com/microsoft/TypeScript/issues/36826
    /**
     * defaultProps are not supported on render functions
     */
    defaultProps?: never;
    /**
     * propTypes are not supported on render functions
     */
    propTypes?: never;
}
Run Code Online (Sandbox Code Playgroud)

差异

  • ForwardRefRenderFunction不支持propTypesand defaultProps,而支持ForwardRefExoticComponent
  • ForwardRefExoticComponent有一个额外的成员$$typeof类型symbol
  • 的调用签名ForwardRefRenderFunction接受一个props对象,该对象必须包含一个成员children和一个引用对象作为参数,而 的调用签名ForwardRefExoticComponent只接受一个任意形状的 props 对象作为参数。

还有一些想法

forwardRef函数的定义中可以最好地看到这两个定义的相互作用:

function forwardRef<T, P = {}>(render: ForwardRefRenderFunction<T, P>): ForwardRefExoticComponent<PropsWithoutRef<P> & RefAttributes<T>>;
Run Code Online (Sandbox Code Playgroud)

此外,这两个定义之间的一个很大区别似乎是,ForwardRefExoticComponent(像所有外来组件一样)不是函数组件,而实际上只是对象,在渲染它们时会对其进行特殊处理。因此评论

注意:Exotic 组件不可调用。

出于某种原因,这些奇特的组件在某些地方是必需的。

  • 如果您使用“forwardRef”,则**必须**使用“ForwardRefExoticComponent”。它只是函数的返回类型。但为了避免这种情况,有两种方法:您可以切换到类组件,据我所知,在其中启用引用而无需执行任何操作,或者您可以将引用定义为组件的普通属性(这不能称为“ref” - 将其命名为“inputRef”、“compRef”等)。这也完全可以正常工作。唯一的缺点是,你不能将你的引用命名为“ref”。 (2认同)