【问题标题】:Whats the difference between ForwardRefExoticComponent and ForwardRefRenderFunction in react?反应中 ForwardRefExoticComponent 和 ForwardRefRenderFunction 有什么区别?
【发布时间】:2020-10-07 05:35:43
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: javascript reactjs typescript react-forwardref


    【解决方案1】:

    ForwardRefExoticComponent

    取自here的定义是

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

    如果你写出来,你会得到

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

    ForwardRefRenderFunction

    取自here的定义是

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

    区别

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

    还有一些想法

    这两个定义的相互作用最好在definition of the forwardRef function 中看到:

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

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

    注意:外部组件不可调用。

    而且出于某种原因,这些奇异的组件在某些地方是必要的。

    【讨论】:

    • 嗨,彼得,感谢您的回答。我可以多提一些你的建议吗?那么使用 ForwardRefExoticComponent 是不是一个坏习惯呢?如果我只是切换到类组件会更好吗,因为我认为类组件中的 ref 会更直接?
    • 如果您使用forwardRef,您必须使用ForwardRefExoticComponent。它只是函数的返回类型。但是为避免这种情况,有两种方法:您可以切换到类组件,据我所知,在其中启用 refs 而无需做任何事情,或者您可以将 ref 定义为组件的普通属性(不能被称为 'ref' - 将其命名为例如 'inputRef'、'compRef' 等)。这也完全正常。唯一的缺点是,您不能将您的 ref 命名为“ref”。
    猜你喜欢
    • 1970-01-01
    • 2013-08-21
    • 2014-10-01
    • 1970-01-01
    • 2021-08-11
    • 2013-06-02
    • 1970-01-01
    • 2020-04-29
    相关资源
    最近更新 更多