根据错误信息,发现在 Props 接口中定义了一个 children 属性,类型为 React.ReactElement,但是在实际使用中,children 的类型为 '(string | Element)[]',这与 Props 中定义的类型不兼容。

要解决这个问题,需要将 Props 中的 children 属性的类型修改为兼容 '(string | Element)[]',或者将实际使用中的 children 的类型修改为 React.ReactElement。

修改后的代码如下:

import {
  ModalForm,
  ProFormDatePicker,
  ProFormSelect,
  ProFormText,
  ProFormTextArea,
} from '@ant-design/pro-form';
import { message } from 'antd';
import React, { useState } from 'react';

interface Props {
  children: (string | Element)[];
}

const ApplicationModal: React.FC<React.PropsWithChildren<Props>> = (props) => {
    const { children } = props;
  const [visible, setVislble] = useState(false);
 
  return (
    <>
      {React.cloneElement(children, {
        onClick: () => {
          setVislble(true);
        },
        type: 'primary',
      })}
      <ModalForm
        title='新建测试申请'
        visible={visible}
        width={600}
        labelCol={{ span: 4 }}
        modalProps={{ 
          destroyOnClose: true,
          onCancel: () => {
            setVislble(false);
          },
        }}
      >
         <ProFormSelect
          name='riskDegree'
          label='测试类型-1级'
        //   request={async () => {
        //     const { data } = await queryDict({ types: ['riskLevelType'] });
        //     return data[0].enumTypeVOS;
        //   }}
          width='xl'
          placeholder='请选择测试类型-1级'
        />
      </ModalForm>
    </>
  );
};
 export default React.memo(ApplicationModal);

请注意,根据实际需求修改 children 属性的类型,以使其与实际使用中的类型一致。

React 类型错误:'children' 属性类型不兼容 - 解决方法

原文地址: https://www.cveoy.top/t/topic/p9im 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录