reactjs - React Antd Modal 属性 'children' 在类型 'IntrinsicAttributes & ModalProps' 上不存在
升级到 React 18 后,我在使用 Antd 时遇到了以下问题。
当使用 Antd 的组件(Modal、Dialog 等)时,它会给我这个错误:
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'.
这是我的组件:
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>
);
}
这里是我的道具:
type Props = {
children: React.ReactNode,
open: boolean,
onClose?: () => void
}
我使用 node 17.8.0
答案#1
你应该使用
"@types/react": "17.0.39"
而不是
"@types/react": "18.0.3"
因为 react18 的 @types 还不兼容。
