且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-12-30 21:12:30

我自己遇到了这个问题,并通过将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 || ''}
  ...
/>