Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use antd.Form.create in typescript?

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 TS2339: Property 'loading' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<Compone nt<{}, ComponentState>> & Readonly<{ childr...'. 

LoginForm.tsx

import * as React from 'react'; import { Form } from 'antd'; import { WrappedFormUtils } from 'antd/lib/form/Form';  interface Props {     form: WrappedFormUtils;     loading: boolean;     username?: string; }  class LoginForm extends React.Component<Props, {}> {     render() {         const { loading } = this.props;         return (<div>form {loading ? 'true' : 'false'}</div>);      } }  export default Form.create()(LoginForm); 

LoginPage.tsx

import LoginForm from './components/loginForm';  const loginPage: React.SFC<Props> = (props) => {      return (          <div>               <LoginForm loading={true}/>                          ^ error here!          </div>      );  }; 

My antd version is 2.11.2


Finally I found a solution

class LoginForm extends React.Component<Props & {form:     WrappedFormUtils}, State> {   render() {     const { loading } = this.props;     return (<div>form {loading ? 'true' : 'false'}</div>);   } }  export default Form.create<Props>()(LoginForm); 
like image 643
Kennir Avatar asked Jul 04 '17 06:07

Kennir


People also ask

What is valuePropName?

valuePropName is the name of the prop of the child component that will contain the value. In most cases, components such as Input or Select use value as the prop for holding the value. However, certain components such as Checkbox or Switch use checked .

How do you display the first item by default in a dynamic antd form?

You have to click the "+ Add Field" button for the dynamic form to add and show the first field as shown here..

How do you use ant design in react typescript?

Install and Initialization Ensure your system has installed latest version of yarn or npm. Create a new cra-template-typescript project named antd-demo-ts using yarn. Then we go inside antd-demo-ts and start it. Open browser at http://localhost:3000/, it renders a header saying "Welcome to React" on the page.


2 Answers

  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.

like image 182
AmazingTurtle Avatar answered Oct 04 '22 01:10

AmazingTurtle


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); 
like image 37
Suben Saha Avatar answered Oct 04 '22 01:10

Suben Saha