我有一个可以接受 Reactchildren作为节点或节点数组的组件。我希望能够检测是否children是一个节点数组,但我收到以下 Typescript 错误:
TS2339:类型“字符串”上不存在属性“长度”| 数量 | 真实| {} | ReactElement<任何,字符串 | ((props: any) => ReactElement<any, any> | null) | (new (props: any) => Component<any, any, any>)> | ... 47 更多... | (ReactNode[] & ReactPortal)'。'number' 类型不存在属性 'length'。
typescript中有没有办法检测children长度?谢谢。
import React from 'react';
interface Props {
children: React.ReactNode | React.ReactNode[];
}
const SampleComponent: React.FC<Props> = ({ children }) => {
if (children && children.length) {
return children.map((child) => (
<div>{child}</div>
));
}
return children;
};
export default SampleComponent;

