使用 setFieldsValue 方法手动设置新的值
示例代码:
import React, { useState } from 'react'; import { Form, Input, Button } from 'antd';
const DynamicForm = () => { const [form] = Form.useForm(); const [initialValues, setInitialValues] = useState({ field1: '', field2: '', });
const handleAddField = () => {
const newField = field${Object.keys(initialValues).length + 1}
;
setInitialValues({
...initialValues,
[newField]: '',
});
};
const handleSubmit = (values) => { console.log(values); };
return (
export default DynamicForm;