Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix it Cannot use `setFieldsValue`

Tags:

antd

How to fix it

Warning: Cannot use setFieldsValue until you use getFieldDecorator or getFieldProps to register it.

like image 526
lesik nu Avatar asked Jan 18 '18 20:01

lesik nu


2 Answers

I faced the same issue with react hooks. Following solution works for me.

props.form.getFieldDecorator('Amount', { initialValue: 'My Value' })

As I feel the reason is initialValue is not going to change after the first render, just like defaultValue.

like image 200
Janith Widarshana Avatar answered Sep 17 '22 14:09

Janith Widarshana


From the doc https://ant.design/components/form/#this.props.form.getFieldDecorator(id,-options):

After wrapped by getFieldDecorator, value(or other property defined by valuePropName) onChange(or other property defined by trigger) props will be added to form controls,the flow of form data will be handled by Form [...]

So, without the registration of the form field with getFieldDecorator, you cannot use setFieldsValue.

You are setting a value to a field that doesn't exist inside the form. First, you must register the field into the form with getFieldDecorator and then you can use the setter.

Maybe the getter-style method name it's a bit misleading.

like image 25
nachì Avatar answered Sep 19 '22 14:09

nachì