用 extends React.Component 替换 React.createClass 但我得到一个 Uncaught TypeError: Cannot read property 'getFieldDecorator' of undefined
使用以下代码:
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>
)
}
}
看起来缺少 Form.create 部分是导致问题的原因,但不知道它适合使用扩展机制的位置。
我怎样才能正确地做到这一点?