Antd Form中动态设置Form.Item的值无效?-灵析社区

你好我叫张倩话

Antd Form 设置动态Item的值无效 ![image.png](https://wmprod.oss-cn-shanghai.aliyuncs.com/c/user/20241003/58f50c6ac55f337acdde78df8053647e.png) 在代码中我动态生成了Form.Item,在Item内设置initialValue无效。 就换了一种方式,使用form.setFieldsValue去设置表单值,在点击确定时,表单有值,也通过了校验, 但就是不显示值,请问是什么原因呢? const RenderForm = () => { const [form] = Form.useForm(); useEffect(() => { if (deviceType) { let map = {}; deviceType.customers.forEach((item) => { map[item.id] = JSON.parse(item.defaultValue); }); console.log('设置表单', map); form.setFieldsValue({ customers: map }); setTimeout(() => { let values = form.getFieldsValue(); console.log('values是', values); }, 1000); } }, [deviceType]); const getFormItems = () => { console.log('deviceType', deviceType); if (deviceType?.customers && deviceType.customers.length > 0) { return deviceType.customers.map((item) => ( {getComponent(item.dataType, JSON.parse(item.dataConfig))} )); } return null; }; return ( {getFormItems()} 取消 确定 ); } getComponent 这个方法里把 Select 和InputNumber封装了一下,所以无法显示值,该怎么避免这个问题呢

阅读量:134

点赞量:0

问AI
PunkMaccccc
虽然不知道原因,但是我把Component封装过的 Select 和Input 添加了一个defaultValue,设置为初始值,就可以了