javascript - Antd 表单初始值在状态更改后不更新

标签 javascript reactjs antd

我有一张表格显示学校名称及其费用或费率。表中有一个按钮可以打开包含此组件或表单的抽屉。表中有一个按钮可以切换抽屉的状态。只要我只是通过单击我的 initalValue 更新按钮来读取表单中的值。但是,如果我更新价格并提交任何 1 个条目的表格,那么我的价格或学校的初始值不会因其他条目或学校而改变。当我的状态发生相应变化时,我确实通过渲染状态进行了检查。但是我的表单初始值没有相应更新


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)

我只想重新呈现完整的表格,以便在我从我的表格中单击其他学校时立即更新数据

最佳答案

要更新 initialValue 表单输入字段,需要清理或重置。 所以在成功提交表单后添加 form.resetFields()(以重置输入字段中的数据)。

关于javascript - Antd 表单初始值在状态更改后不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65382963/

相关文章:

antd - Ant design 输入验证只接受字母和特殊字符,但不接受数字

javascript - 解码 base64 字符串在 JS 中不起作用,适用于 Linux 的 base64

javascript - 测试/导航其他网站的最佳语言

JavaScript -- 切片时间字符串返回 HH :MM

javascript - 未捕获( promise )TypeError : Already read

javascript - 单击 react/next.js 上的 anchor 链接时平滑滚动

reactjs - 使用electron + react.js,组件不会在生产版本中呈现

javascript - 从 Web 组件获取数据,该数据包含在另一个 Web 组件中。 polymer

reactjs - 语法错误: Unexpected end of JSON input while parsing near '...-env" :"^7.4.4" ,"@babe'

javascript - Ant Design Upload 获取文件内容