问题
I am using ant-design library. There is useForm()
hook to reset values.
All values are being reset to original values but not for Radio.Group.
How do I reset my radio button state to priority
value from State?
const [priority, setPriority] = useState(2);
const [form] = Form.useForm();
const onSubmit = (values) => {
console.log('Received values of form: ', values);
form.resetFields();
}
<Form
form={form}
labelCol={{ span: 4 }}
wrapperCol={{ span: 14 }}
layout="horizontal"
initialValues={{ size: "large" }}
size={"large"}
onFinish={onSubmit}
>
<Form.Item
name="title"
rules={[{ required: true, message: 'Title is required!' }]}
label="Title">
<Input placeholder="Title" />
</Form.Item>
<Form.Item
name="content"
rules={[{ required: true, message: 'Content is required!' }]}
label="Content">
<Input.TextArea placeholder="Content" />
</Form.Item>
<Form.Item label="Switch" name="switch">
<Switch />
</Form.Item>
<Form.Item name="priority" label="Radio.Button">
<Radio.Group defaultValue={priority}
onChange={onPriorityChange}>
<Radio.Button value={2}>High</Radio.Button>
<Radio.Button value={1}>Medium</Radio.Button>
<Radio.Button value={0}>Low</Radio.Button>
</Radio.Group>
</Form.Item>
<Form.Item>
<Button type="primary" htmlType="submit">
Submit
</Button>
</Form.Item>
</Form>
回答1:
I added priority to the initialValues and it worked.
Refer this.
https://codesandbox.io/s/elegant-agnesi-9vp4x?file=/src/App.js
来源:https://stackoverflow.com/questions/61720143/reset-radio-group-values-onsubmit-click-for-antd-library