Ngo*_*Lam 10 javascript typescript reactjs react-forwardref
我正在编写一个 React 组件,它可以将 ref 转发给它的孩子
我发现对于函数组件的返回类型,我可以使用ForwardRefExoticComponent和ForwardRefRenderFunction。但我不确定它们之间有什么区别。
到目前为止,当使用ForwardRefExoticComponent 时,我可以扩展它而ForwardRefRenderFunction不能?我在这里发布了一个与我的案例相关的问题:How to export forwardRef with ForwardRefRenderFunction
如果有人知道他们之间的区别以及他们所做的事情,请帮助我。因为 React 团队似乎没有关于它们的文档(但它们在 react 包中)
Pet*_*rdt 10
从这里采取的定义是
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类型symbolForwardRefRenderFunction接受一个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 组件不可调用。
出于某种原因,这些奇特的组件在某些地方是必需的。
| 归档时间: |
|
| 查看次数: |
4664 次 |
| 最近记录: |