想要使用form.setFieldsValue就需要实例出form对象,但是如果是如下的嵌套组件结构该怎么传递呢,antd官方给我们提供了方法
const [form] = Form.useForm()
...
...
子组件则只需要使用官方提供的Form.useFormInstance()即可获得上下文中的form
...// 获取当前上下文正在使用的 Form 实例const form = Form.useFormInstance();
...// 改变对应input的值 const profit = () => {// setFieldsValue({name:"value"})form.setFieldsValue({SCProfitMargin: '11111', // 对应第一个SCProfitMargin inputVCProfitMargin: '22222', // 对应第二个VCProfitMargin inputSCendProfitMargin: "3333333",VCendProfitMargin: "444444"})}
...
当你使用form.setFieldsValue重新设置input的值的时候,他也许不会生效,看看你的html结构是否是这样的
这样的结构
Form.Item不会关联到指定input,所以官方又提供了一个新的方案
required: true }]}>lowPriceOnblur}style={{ width: '185px', height: '32px' }}defaultValue="默认值"/>
更改后
noStyle是为了让内层
Form.Item失去样式不造成冲突
required: true }]} >lowPriceOnblur}style={{ width: '185px', height: '32px' }}defaultValue="默认值"/>
上一篇:不忘沟壑是褒义词还是贬义词