Update antd form if initialValue is changed

If you are using hooks you can add an useEffect with the code:

useEffect(() => {
 form.setFieldsValue(defaultValues)
}, [form, defaultValues])

Remember define the form value first:

const [form] = Form.useForm()

And remember to pass this form instance to the Form component:

<Form
    form={form}
    layout="vertical"
    name="form-name"
    initialValues={defaultValues}
    onFinish={submitHandler}
  >

In this form, you going to update the defaultValues when the component renders.
If you are using classes, you can put the code inside componentDidUpdate lifecycle method.

Regards

Leave a Comment