-
Hey there I am using the react-select as part of my search form. I'd like to be able to clear the select fields in a simple function once I reset the form using a reset input field. Could someone show me how to do this? Thanks!
|
Beta Was this translation helpful? Give feedback.
Answered by
ebonow
Jan 8, 2021
Replies: 1 comment 2 replies
-
Greetings @hermbo , There are two ways to clear the Select.
const [ value1, setValue1 ] = useState();
const [ value2, setValue2 ] = useState();
handleReset = () => {
setValue1(null);
setValue2(null);
}
<form onReset={this.handleReset}>
<Select onChange={opt => setValue1(opt)} value={value1} options={options} />
<Select onChange={opt => setValue2(opt)} value={value2} options={options2} />
<input type="reset" value="Reset" />
</form>
const select1Ref = useRef();
const select2Ref = useRef();
handleReset = () => {
select1Ref.current.select.clearValue();
select2Ref.current.select.clearValue();
}
<form onReset={this.handleReset}>
<Select ref={select1Ref} options={options} />
<Select ref={select2Ref} options={options2} />
<input type="reset" value="Reset" />
</form> |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
hermbo
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Greetings @hermbo ,
There are two ways to clear the Select.