老师您好:
我的问题如下:
const renderChildren = () => {
return React.Children.map(children, (child, index) => {
const childElement = child as React.FunctionComponentElement<MenuItemProps>;
// 这里直接取displayName不会报错,但是最终获取到的displayName为undefined,但是取name是ok的
const { name } = childElement.type;
if (name === 'MenuItem' || name === 'SubMenu') {
return React.cloneElement(childElement, {
index: childElement.props.index ? childElement.props.index.toString() : index.toString()
});
} else {
console.error("Warning: Menu has a child which is not a MenuItem component");
}
})
}
然后我打印了一下type的类型,确认是function,但是还是通过直接读取childElement.type返回了一个有name属性的对象,且name的属性值确实是MenuItem,debug到这里着实有点不太明白了,希望老师指导一下,感谢~