[英]React Antd Modal Property 'children' does not exist on type 'IntrinsicAttributes & ModalProps'
After upgrading to React 18 I have the following issue with Antd.升级到 React 18 后,我遇到了 Antd 的以下问题。
When using a component from Antd (Modal, Dialog, ...) it gives me this error:当使用来自 Antd 的组件(模态,对话框,...)时,它给了我这个错误:
TS2322: Type '{ children: ReactNode; ref: ForwardedRef<unknown>; className: string; visible: true; onCancel: (() => void) | undefined; onOk: undefined; footer: null; closable: false; }' is not assignable to type 'IntrinsicAttributes & ModalProps'. Property 'children' does not exist on type 'IntrinsicAttributes & ModalProps'.
Here is my component:这是我的组件:
function TransitionsModal(props: Props): JSX.Element {
const {
open,
onClose,
children
} = props;
return (
<div>
{open &&
<Modal
className="flex items-center justify-center"
visible={open}
onCancel={onClose}
onOk={undefined}
footer={null}
closable={false}>
{children}
</Modal>
}
</div>
);
}
And here my Props:这里是我的道具:
type Props = {
children: React.ReactNode,
open: boolean,
onClose?: () => void
}
I use node 17.8.0我使用节点 17.8.0
you should use你应该使用
"@types/react": "17.0.39"
instead of代替
"@types/react": "18.0.3"
because react18 @types is not yet compatible.因为 react18 @types 还不兼容。
Issue is related to latest updates of types in react 18. In order to avoid this error if you use yarn one solution would be be to use resolutions in package.json with some lower version of react.问题与 react 18 中类型的最新更新有关。如果您使用 yarn,为了避免此错误,一种解决方案是使用 package.json 中的分辨率和一些较低版本的 react。 This way your dependency will be instructed to use types form diff version.
这样,您的依赖项将被指示使用不同版本的类型。
Example:例子:
"resolutions": { "@types/react": "17.0.30" }
I prefer using this comment to ignore TS error instead of downgrading types我更喜欢使用此注释来忽略 TS 错误而不是降级类型
const MasonryBrick: FC<PostCardType> = ({ imgSrc, title, excerpt }) => {
return (
// @ts-ignore this lib is incompatible with react18
<Slide triggerOnce direction="up">
<div className={s.context}>
<h2>{title}</h2>
<p>{excerpt}</p>
</div>
</Slide>
);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.