Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to Get value of select onChange in Functional Component in React

Im Using ant design pro with react to create form, but i cannot save the value of select using hooks. i Shows a errorn errorenter image description here

const ExistingGroup = (props) => {
  const [group,setGroup] = useState([]);

  const saveGroup = (event) => {
    setGroup(event.target.value);
  }
  return (
    <PageContainer header={{ title: '' }} ghost>
      <Card bordered={false} style={{ width: '100%' }}>
        <Form>
          <Form.Item>
            <Select value={group} onSelect={saveGroup}>
              <Option value='1'>1</Option>
              <Option value='2'>2</Option>
            </Select>
            {group}
          </Form.Item>
        </Form>
      </Card>
    </PageContainer>
  );
};

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If you read the documentation of Select, the onSelect method is called with the value of the selected element.

onSelect Called when an option is selected, the params are option’s value (or key) and option instance
function(string | number | LabeledValue, option: Option)

so you want

const saveGroup = (value) => {
  setGroup(value);
}

Also, since it is not multiselect, you should use a single value for the group and not an array.

const [group,setGroup] = useState(null);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading