如何以编程方式清除/重置React-Select? [英] How to programmatically clear/reset React-Select?
问题描述
ReactSelect V2 和 V3 似乎有几个道具,例如clearValue
,resetValue
和setValue
.无论我在尝试什么,都无法以编程方式清除选择. 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 || ''}
...
/>
这篇关于如何以编程方式清除/重置React-Select?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!