ComponentType和Component

来源:4-9 【高阶组件HOC】withAddToCart()

Boy越越

2021-06-10

老师的代码中const withAddToCart = (ChildComponent:React.ComponentType)=>{}
请问可以写成const withAddToCart = (ChildComponent:React.Component)=>{} 吗?
React.Component和React.ComponentType区别是什么?

写回答

1回答

阿莱克斯刘

2021-06-11

有两篇文章我们可以参考一下:

1. 第一篇:https://flow.org/en/docs/react/types/

//img.mukewang.com/szimg/60c2859f094798e718020556.jpg

文章的这个部分会告诉我们componentType是用于修饰HOC的。

2. 第二篇文章:

https://medium.com/@jrwebdev/react-higher-order-component-patterns-in-typescript-42278f7590fb

图片中的段落告诉我们componentType是FC和class Compnonet的集合

//img.mukewang.com/szimg/60c2859f09f6342214760384.jpg

如果打开源码,我们也可以得到印证:

//img.mukewang.com/szimg/60c2859f09aa7c1616400174.jpg




3
0

React18 系统精讲 结合TS打造旅游电商平台

React18 精讲 + 结合 TS 实战 + 热门业务开发,获取必备技能

1993 学习 · 1015 问题

查看课程