[英]Correctly creating an antd Form using the extends React.Component mechanism
I am trying to reproduce the antd Form example in https://github.com/ant-design/ant-design/blob/master/components/form/demo/horizontal-login.md 我试图在https://github.com/ant-design/ant-design/blob/master/components/form/demo/horizontal-login.md中重现antd Form示例
Replacing React.createClass with extends React.Component but I am getting a Uncaught TypeError: Cannot read property 'getFieldDecorator' of undefined 用扩展React.Component替换React.createClass但是我得到一个Uncaught TypeError:无法读取未定义的属性'getFieldDecorator'
with the following code: 使用以下代码:
import { Form, Icon, Input, Button } from 'antd';
const FormItem = Form.Item;
export default class HorizontalLoginForm extends React.Component {
constructor(props) {
super(props);
}
handleSubmit(e) {
e.preventDefault();
this.props.form.validateFields((err, values) => {
if (!err) {
console.log('Received values of form: ', values);
}
});
},
render() {
const { getFieldDecorator } = this.props.form;
return (
<Form inline onSubmit={this.handleSubmit}>
<FormItem>
{getFieldDecorator('userName', {
rules: [{ required: true, message: 'Please input your username!' }],
})(
<Input addonBefore={<Icon type="user" />} placeholder="Username" />
)}
</FormItem>
<FormItem>
{getFieldDecorator('password', {
rules: [{ required: true, message: 'Please input your Password!' }],
})(
<Input addonBefore={<Icon type="lock" />} type="password" placeholder="Password" />
)}
</FormItem>
<FormItem>
<Button type="primary" htmlType="submit">Log in</Button>
</FormItem>
</Form>
)
}
}
Looks like the missing Form.create part is causing the issue, but don't know where it fits using the extends mechanism. 看起来缺少Form.create部分导致问题,但不知道它使用扩展机制适合哪里。
How can I properly do it ? 我该怎么做呢?
@vladimirimp is on the right track, but there are 2 issues with the chosen answer. @vladimirimp走在正确的轨道上,但所选答案有2个问题。
Form.create()
) should not be called in the render method. 不应在render方法中调用高阶组件(如Form.create()
)。 myHorizontalLoginForm
) to start with capital letters. JSX要求用户定义的组件名称(例如myHorizontalLoginForm
)以大写字母开头。 To fix this, we just need to change our default export of HorizontalLoginForm
: 要解决这个问题,我们只需要更改HorizontalLoginForm
的默认导出:
class HorizontalLoginForm extends React.Component { /* ... */ }
export default Form.create()(HorizontalLoginForm);
Then we can use HorizontalLoginForm
directly without needing to set it to a new variable. 然后我们可以直接使用HorizontalLoginForm
而无需将其设置为新变量。 (but if you did set it to a new variable, you would want to name that variable MyHorizontalLoginForm
or anything else that starts with a capital letter). (但如果您确实将其设置为新变量,则需要将该变量MyHorizontalLoginForm
或以大写字母开头的任何其他内容)。
When you wish to include your form class in parent component you must first create form, for example in parent components render method: 当您希望在父组件中包含表单类时,必须首先创建表单,例如在父组件渲染方法中:
...
render() {
...
const myHorizontalLoginForm = Form.create()(HorizontalLoginForm);
...
return (
...
<myHorizontalLoginForm />
)
}
Be sure to import your HorizontalLoginForm class in the parent class. 请务必在父类中导入HorizontalLoginForm类。
You can study official example: https://ant.design/components/form/#components-form-demo-advanced-search 您可以学习官方示例: https : //ant.design/components/form/#components-form-demo-advanced-search
@vladimirp it is not good to call Form.create
in render. @vladimirp在渲染中调用Form.create
是不好的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.