简体   繁体   English

React-Select:defaultValue属性中的多个值

[英]React-Select: Multiple values in defaultValue attribute

Im trying to add multiple values inside my defaultValue attribute but without success. 我试图在我的defaultValue属性内添加多个值,但没有成功。

<Select 
   value={this.state.selectedTests} 
   defaultValue={this.getDefaultValues(this.props.tests)} 
   isMulti onChange={(e) => this.handleChange(e)} 
   options={this.state.allTests} 
   className="basic-multi-select dropDownSelector"
 />

getDefaultValues() returns an array: getDefaultValues()返回一个数组:

0:{value: "t1", label:"Test1"}
1:{value: "t2", label:"Test2"}

But the Select element shows no labels... 但是Select元素没有显示标签...

Anyone else faced the same issue or knows how to solve this? 其他人也面临同样的问题,或者知道如何解决吗?

Thank you for your help! 谢谢您的帮助!

I solved my problem :) The values in the attribute "value" overwrote the values in the attribute "defaultValue". 我解决了我的问题:)属性“值”中的值覆盖了属性“ defaultValue”中的值。 I delete the attribute "value" and it works now. 我删除了属性“值”,它现在可以使用。

Changed code: 更改的代码:

<Select 
   defaultValue={this.getDefaultValues(this.props.tests)} 
   isMulti onChange={(e) => this.handleChange(e)} 
   options={this.state.allTests} 
   className="basic-multi-select dropDownSelector"
 />

You should only provide the value in the defaultValue prop. 您只应在defaultValue属性中提供值。 The label is located in the options prop. 标签位于选项属性中。 It will match the correct value with the prop that you provided in the options prop. 它将与选项prop中提供的prop匹配正确的值。 The method getDefaultValues() should return: 方法getDefaultValues()应该返回:

0: "t1",
1: "t2"

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

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