'Antd form initial value not updating after state change

I have a table showing school name and its fee or rates. There is button in table which opens a drawer containing this component or form.There is a button in table which toggles the state of drawer. As long as i am only reading values in the form by clicking on the button my initalValue updates. But if i update price and submit form for any 1 entry , then initial value my price or school doesn't change for other entries or schools. While my state is changing accordingly which I did check by rendering state. But my initialValue of form doesn't updating accordingly


import React, { useState, useEffect } from 'react'
import { Table, Button, Popconfirm, Drawer, Form, Input, Select, notification } from 'antd'

const { Option } = Select

const layout = {
  labelCol: {
    span: 10,
  },
  wrapperCol: {
    span: 10,
  },
}
const tailLayout = {
  wrapperCol: {
    offset: 7,
    span: 14,
  },
}


function SchoolRates({ form, relevantData }) {
  const [rates, setRates] = useState(null)

  useEffect(() => {
   setRates(relevantData)
}, [someCondition])

  const updateRates = e => {
    e.preventDefault()
    form.validateFields((error, values) => {
      console.log(error, values, 'forms')
    })
    return 'Data Updated'
  }

  return (
    <>
      <div>
        {`${rates?.schoolName}, ${rates?.Price}`}
      </div>
      <Form {...layout} initialValue={rates} onSubmit={e => updateRates(e)}>
        <Form.Item label="Country" style={{ marginBottom: '5px' }} size="small">
          {form.getFieldDecorator('schoolName', {
            initialValue: rates?.schoolname,
            rules: [{ required: true, message: 'Please select a school!' }],
          })(
            <Select
              placeholder="School"
              allowClear
              size="small"
              style={{ borderRadius: 0 }}
            >
              {schoolList.map(item => (
                <Option value={item}>{item}</Option>
              ))}
            </Select>,
          )}
        </Form.Item>
        <Form.Item label="Price / Learner" style={{ marginBottom: '5px' }} size="small">
          {form.getFieldDecorator('price', {
            initialValue: rates?.price,
            rules: [{ required: true, message: 'Please enter price/learner' }],
          })(
            <Input
              size="small"
              type="number"
              style={{ borderRadius: 0 }}
            />,
          )}
        </Form.Item>
        <Form.Item {...tailLayout}>
          <Button type="primary" htmlType="submit" size="small">
            Save
          </Button>
        </Form.Item>
      </Form>
    </>
  )
}

export default Form.create()(UpdateRates)

I just want to re render the complete form to update data as soon as I click on other school from my table



Solution 1:[1]

To update initialValue form input fields need to be cleaned or reset. So add form.resetFields()(to reset data in input fields) after successful submission of form.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 devspartan