我試圖重現https://github.com/ant-design/ant-design/blob/master/components/form/demo/horizontal-login.md使用擴展React.Component機制
與更換React.createClass延伸React.Component的antd表例子正確創建antd形式,但我得到一個遺漏的類型錯誤:無法讀取屬性未定義
「getFieldDecorator」用下面的代碼:
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部分導致了問題,但不知道它適合使用的擴展機制。
我該如何正確地做到這一點?
不要在render方法中使用HOC。在渲染中調用HOC會導致組件在每個渲染上重新裝入。 [記錄在這裏](https://github.com/facebook/react/blob/044015760883d03f060301a15beef17909abbf71/docs/docs/higher-order-components.md#dont-use-hocs-inside-the-render-method)以及[ (https://stackoverflow.com/q/39120494/2479481)。 –