typescript 如何在打字稿中使用 antd.Form.create?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44898248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use antd.Form.create in typescript?
提问by Kennir
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
我有一个由 Form.create() 创建的登录表单,但是我无法从父组件向这个表单传递任何道具,编译器总是通知一个错误,如
error TS2339: Property 'loading' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<Compone
nt<{}, ComponentState>> & Readonly<{ childr...'.
LoginForm.tsx
登录表单.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
登录页面.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
我的antd版本是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);
回答by AmazingTurtle
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.
导入 FormComponentProps
import {FormComponentProps} from 'antd/lib/form/Form';
然后让你的组件
interface YourProps { test: string; } class YourComponent extends React.Component<YourProps & FormComponentProps> { constructor(props: YourProps & FormComponentProps) { super(props); ... } }
然后使用 Form.create() 导出类
export default Form.create<YourProps>()(YourComponent);
Form.create 上的通用参数将结果转换为带有 YourProps 的 React ComponentClass - 没有 FormComponentProps,因为它们是通过 Form.create 包装器组件注入的。
回答by Suben Saha
I got a better approach from antddocumentation
我从antd文档中得到了更好的方法
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);