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...'.
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);
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);
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 .
You have to click the "+ Add Field" button for the dynamic form to add and show the first field as shown here..
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.
Import the FormComponentProps
import {FormComponentProps} from 'antd/lib/form/Form';
Then have your component
interface YourProps { test: string; } class YourComponent extends React.Component<YourProps & FormComponentProps> { constructor(props: YourProps & FormComponentProps) { super(props); ... } }
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With