How to use antd.Form.create in typescript?

后端 未结 2 1345
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-14 06:25

I have a login form created by Form.create(), but I can\'t pass any props to this form from parent component, compiler always notify a error like

error TS23         


        
相关标签:
2条回答
  • 2020-12-14 06:58

    I got a better approach from antd documentation

    import { Form } from 'antd';
    import { FormComponentProps } from 'antd/lib/form';
    
    interface UserFormProps extends FormComponentProps {
      age: number;
      name: string;
    }
    
    class UserForm extends React.Component<UserFormProps, any> {
      // ...
    }
    
    const App = Form.create<UserFormProps>({
      // ...
    })(UserForm);
    
    0 讨论(0)
  • 2020-12-14 07:15
    1. Import the FormComponentProps

      import {FormComponentProps} from 'antd/lib/form/Form';
      
    2. Then have your component

      interface YourProps {
          test: string;
      }        
      
      class YourComponent extends React.Component<YourProps & FormComponentProps> {
          constructor(props: YourProps & FormComponentProps) {
              super(props);
              ...
          }
      }
      
    3. Then export the class using Form.create()

      export default Form.create<YourProps>()(YourComponent);
      

      The generic argument on Form.create casts the result to a React ComponentClass with YourProps - without FormComponentProps, because these are being injected through the Form.create wrapper component.

    0 讨论(0)
提交回复
热议问题