Reset Radio.Group values onSubmit click for antd library

随声附和 提交于 2020-05-17 06:31:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!