renderChildren = () => {
const { children, divider } = this.props;
return (
React.Children.map(children, (child, index) => (
<>
{child}
{(index < children.length - 1) && divider}
</>
))
);
}
更新:
const children: React.ReactNode Object is possibly 'null' or 'undefined'.ts(2533)
不在此答案的范围内,但可以处理此情况
renderChildren = () => {
const { children, divider } = this.props;
if (children) { // this fixes error above
return (
React.Children.map(children, (child, index) => (
<>
{child}
{(index < children.length - 1) && divider}
</>
))
);
}
return null
}
出现该错误是因为您将prop定义为children?: ReactNode
,因为它是可选的TypeScript可以使您免受未定义的值的影响
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句