Umi初始化项目引入Antd组件报错"xxx"不能用作 JSX 组件
问题: 在使用umi初始化项目后, 所有从antd引入的组件全部报"xxx"不能用作 JSX 组件
这个错误
例如:
“Button”不能用作 JSX 组件。
其返回类型 "ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => >Component<any, any, any>)>) | (new (props: any) => Component<any, any, any>)>" 不是有效的 JSX 元素。
类型“ReactElement<any, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, > any, any>)>) | (new (props: any) => Component<any, any, any>)>”缺少类型“Element”中的以下属性: nodeName, attributes, childrents(2786)
原因: 因为你的其他库依赖了@types/react或者@types/react-dom,比如项目安装了react-router这个库, 而这个库又安装了18版本的@types/react,导致多个ts校验不通过。
解决方法: 尝试在package.json里面添加以下配置:版本改为你指定的版本就好
"resolutions": {
"@types/react": "17.0.0",
"@types/react-dom": "^17.0.0"
},