关于children的类型
来源:11-2 【布局重构】页面布局

慕仔7463560
2022-06-28
老师 关于这个children的类型 我试着改成React.ReactElement
或者JSX.Element
都会报错
对于父组件来说children都不应该只算做一个子级吗
我又试着改成ReactChild
这个类型 报了这个错误 而且这个类型好像被弃用了 children
的类型必须要用React.ReactNode
吗
还想顺便问一下ReactFragment
类型 迭代ReactNode
类型的话会出现 ReactFragment = ReactFragment
的情况吗
写回答
1回答
-
阿莱克斯刘
2022-06-29
第一个问题,children的类型。最新版本的react把children中props的ts默认类型中移除了,所以,在 react 18 中使用children必须得加上类型定义。最好使用 React.ReactNode 类型。
如果不使用typescript,那就没有这个问题。children依然是存在于props中的,react移除的只是children的类型定义而已。
第二个问题,我不是很明白,可以详细描述一下你的问题吗?
00
相似问题