如何以编程方式清除/重置React-Select?

如何以编程方式清除/重置React-Select?

问题描述:

ReactSelect V2 V3 似乎有几个道具,例如clearValueresetValuesetValue.无论我在尝试什么,都无法以编程方式清除选择. resetValue似乎无法从外部访问.

ReactSelect V2 and V3 seems to have several props like clearValue, resetValue and setValue. Whatever I'm trying, I'm not able to clear the selections programmatically. resetValue seems not to be accessible from the outside.

selectRef.setValue([], 'clear')
// or
selectRef.clearValue()

这不会清除当前选择.

我在这里错过了什么吗?还是尚未完全实施?

Do I miss something here or is it not fully implemented yet?

我自己遇到了这个问题,并通过将key传递给React-Select组件并附加了选定的值来设法解决了这个问题.然后这将强制到当选择被更新重新渲染本身.

I came across this problem myself and managed to fix it by passing a key to the React-Select component, with the selected value appended to it. This will then force the ReactSelect to re-render itself when the selection is updated.

我希望这对某人有帮助.

I hope this helps someone.

import ReactSelect from 'react-select';

...

<ReactSelect
  key={`my_unique_select_key__${selected}`}
  value={selected || ''}
  ...
/>