我在整个 Typescript 应用程序中使用 React Functional 组件。
我正在尝试动态更新另一个组件中的一些子组件,如下所示:
const memoizedIterateAndAddProps = useCallback(
(kids: ReactNode) => {
return React.Children.map(kids, (child) => {
if (React.isValidElement(child)){
console.log(child.type.name)
}
}
}, [])
但是typescript一直在抱怨 Property 'name' does not exist on type 'string | JSXElementConstructor<any>'. Property 'name' does not exist on type 'string'.ts(2339)
我知道这与一个孩子可能只是一个字符串的事实有关,但我无法找到一种让 Typescript 开心的解决方法。
我会非常感谢任何人的帮助