如何以编程方式清除/重置React-Select? [英] How to programmatically clear/reset React-Select?

查看:581
本文介绍了如何以编程方式清除/重置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 || ''}
  ...
/>

这篇关于如何以编程方式清除/重置React-Select?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆