Ant Design中getFieldDecorator的使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/webofrxy/article/details/86688389

原因

在使用ant design进行react项目开发时,我们经常会用到form表单组件,在刚开始使用时,可能会遇到这样一个问题:Warning: You cannot set a form field before rendering a field associated with the value.,这个问题是由于Form 组件中Form.Item的初始值的设置造成的。

解决办法

使用initialValue: value来设置你的参数的初始值

 <Form.Item
     {...formItemLayout}
     label="Select"
     hasFeedback
 >
     {getFieldDecorator('select_order', {
         initialValue:this.state.status,
         rules: [
             { required: true, message: '请选择订单状态' },
         ],
 })(
     <Select placeholder="请选择订单状态">
         <Option value="0">未付款</Option>
         <Option value="1">已付款</Option>
         <Option value="2">未发货</Option>
     </Select>
 )}
 </Form.Item>

注意

如果已经设置了initialValue:value,避免在componentDidMount等生命周期函数中使用this.props.form.setFieldsValue

猜你喜欢

转载自blog.csdn.net/webofrxy/article/details/86688389