简体   繁体   English

如何从 react-select multi select 中删除 null 选定值

[英]How to remove null selected values from a react-select multi select

I have been trying to set up react-select but when I enable isMulti there is always two selected blank values in the selection box.我一直在尝试设置react-select但是当我启用isMulti时,选择框中总是有两个选定的空白值。

对话框中两个空白值的图像

I have tried setting default values, using state to control the Select but it keeps showing these two blank values when I load the page.我已经尝试设置默认值,使用 state 来控制Select但是当我加载页面时它一直显示这两个空白值。 Here are some code snippets of the things I have tried:以下是我尝试过的一些代码片段:

<Select
    isMulti
/>
<Select
    isMulti
    defaultValue={[]}
/>
<Select
    isMulti
    options={majors().map((element: any) => {
                return { value: element[2], label: element[2] }
             })}
/>
<Select
    isMulti
    defaultValue={[]}
    options={majors().map((element: any) => {
                return { value: element[2], label: element[2] }
             })}
/>
interface Option {
  value: String,
  label: String
}

let def : Option[] = []

<Select
    isMulti
    defaultValue={def}
    options={majors().map((element: any) => {
                return { value: element[2], label: element[2] }
             })}
/>

Not entirely sure the reason for this, but what ended up fixing the problem was wrapping the <Select> inside of a <div> .不完全确定这样做的原因,但最终解决问题的方法是将<Select>包装在<div>中。

This did the trick:这成功了:

<div>
<Select
    isMulti
    defaultValue={[]}
    options={majors().map((element: any) => {
                return { value: element[2], label: element[2] }
             })}
/>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM