Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用react select not working设置默认值_Javascript_Arrays_Reactjs_React Select - Fatal编程技术网

Javascript 使用react select not working设置默认值

Javascript 使用react select not working设置默认值,javascript,arrays,reactjs,react-select,Javascript,Arrays,Reactjs,React Select,我有一个简单的React组件,它获得一个初始状态: this.state = { currentObject: { isYounger: false } } 然后,我使用默认值this.state.currentObject.isYounger呈现一个react select: <Select name={"age"} value={this.state.currentObject.isYounger} onChange={ne

我有一个简单的React组件,它获得一个初始状态:

this.state = {
  currentObject: {
    isYounger: false
  }
}
然后,我使用默认值this.state.currentObject.isYounger呈现一个
react select

    <Select
      name={"age"}
      value={this.state.currentObject.isYounger}
      onChange={newValue => this.addIsYoungerValue(newValue)}
      options={isYoungerOptions}
    />
this.addIsYoungerValue(newValue)}
选项={isYoungerOptions}
/>
由于某些原因,未设置该值。为什么会这样

版本:


“反应选择”:“^2.4.2”,

这里的问题不是状态选择,实际问题是标签没有显示出来

因此,根据您的
addIsYoungerValue
函数,您正在将
this.state.currentObject.isYounger的值设置为整个对象。i、 e.
{value:true,标签:“Younger”}
。因此,可以通过将初始状态的值更改为以下值来解决该问题

this.state = {
      array: [],
      currentObject: {
        isYounger: { value: true, label: "Younger" }
      }
    };

和hurrey,将显示默认值标签。

您的
defaultValue
value
必须是对象。在您这样的情况下:

defaultValue={isYoungerOptions[0]}


这里有一个。

有另一种方法可以使用值作为默认值。在我的例子中,我使用了“id”和“行业”而不是“标签”和“价值”

在选择组件中:-

<Select 
    name="interested_industries"
    options={industries}
    value={interested_industries}
    getOptionLabel={ x => x.id}
    getOptionValue={ x => x.industry}
    onChange={this.handleSelect}
    isMulti
/>
x.id}
getOptionValue={x=>x.industry}
onChange={this.handleSelect}
伊斯穆利
/>

您可以指定您使用的react select版本吗?谢谢@Dharavhol,我已经用该版本更新了OP。您似乎遇到了问题,因为您试图设置
布尔值。您能否使用字符串值尝试一次,以便我们了解根本原因并帮助解决问题@A7DCThanks@Dharaviol,我已经更新为使用字符串值而不是布尔值,但这没有任何作用。我已经用一个可编辑的沙盒更新了OP,您还可以提供
defaultValue
prop。如本文所述。
this.state = {
     interested_industries: [{id:"ANY", industry:"ANY"}]
}
<Select 
    name="interested_industries"
    options={industries}
    value={interested_industries}
    getOptionLabel={ x => x.id}
    getOptionValue={ x => x.industry}
    onChange={this.handleSelect}
    isMulti
/>