Skip to content

How to clear the select fields externally? #4352

Answered by ebonow
hermbo asked this question in Q&A
Discussion options

You must be logged in to vote

Greetings @hermbo ,

There are two ways to clear the Select.

  1. Make it a controlled input and pass null as the value.
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>
  1. Create a ref and access the internal method.
const select1Ref = useRef();
const select2Ref = useRef();

handleReset = () => {
  select1Ref.current.select.clearValue();
  select2Ref.current.

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@hermbo
Comment options

@EligibleDev
Comment options

Answer selected by hermbo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants