I'm using https://ant.design/components/select/
How can I programmatically remove the selected items from <Select>
?
Note: the <Option>
is not a string value, but a Node.
Clear the value of the Selection field by setting it to null programmatically (e.g. via Button). The value will be set to null but the Selection field will still display the most recent selected value. Clearing the field is only possible via the allow clear user interaction button.
By clicking on the clear icon which is shown in DropDownList element, you can clear the selected item in DropDownList through interaction.
passing null in value attribute of the react-select will reset it.
To select a default option in React, the selected attribute is used in the option element. In React, though, instead of using the selected attribute, the value prop is used on the root select element. So, you can set a default value by passing the value of the option in the value prop of the select input element.
If you are using React Hooks, use the following:
import React, { useState } from 'react'
import { Button, Select } from 'antd'
const { Option } = Select
// inside your component
const ComponentName = () => {
const [selected, setSelected] = useState()
// handler
const clearSelected = () => {
// this line will clear the select
// when you click on the button
setSelected(null)
}
// in the return value
return (
<>
// ...
// In the select element
<Select style={{ width: 150 }} onChange={value => setSelected(value)}
value={selected}>
<Option value="jack">Jack</Option>
<Option value="lucy">Lucy</Option>
</Select>
<Button onClick={clearSelected}>Clear Selected</Button>
</>
)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With