简体   繁体   English

如何在 react-select 中使用字符串数组作为选项

[英]How to use a array of strings as a options in the react-select

I am new to react .我是新来的react I am using react select for the select.我正在使用react select作为选择。 Now, I have the following jsx现在,我有以下jsx

div className="col-md-4 d-flex align-items-center" style={borderClass}>
                <label className="mb-0 font-weight-bold" style={labelFont}>
                  Technology
                                </label>
                <Select
                  styles={styles}
                  options={this.props.technology}
                  placeholder="None Selected"
                />
              </div>


const mapStateToProps = (state) => {
  return {
    technology : state.CreateJob.technology,
    userCompany: state.CreateJob.userCompany
  }
}

this is coming from the reducer as a prop.这是来自减速器作为道具。 Now, the data I am getting is like ,现在,我得到的数据就像,

['a', 'b', 'c']

So, How can I use this as a option in the render .那么,我如何在渲染中使用它作为一个选项。 Can any one help me with this ?谁能帮我这个 ? Thanks.谢谢。

You can render a list like this :您可以呈现这样的列表:

var array1 = ['a', 'b', 'c'];
var technologyList = [];
array1.forEach(function(element) {
    technologyList.push({ label:element, value: element })
});

And use it:并使用它:

<Select options={ technologyList } />
<Select 
options={options} 
getOptionLabel={option => option} 
getOptionValue={option => option}
/>

React select expect options props as array of object with property value and label反应选择期望选项道具作为具有属性值和标签的对象数组

 <Select
   styles={styles}
   options={this.props.technology.map(t=>({value: t, label: t}))}
   placeholder="None Selected"
 />

React-Select expects an array of objects for options with this format: React-Select 期望具有以下格式的选项的对象数组:

[..., { value: 'optionValue', label: 'optionLabel' }, ...]

The label property is displayed to the user and value will be sent to server on form submit.标签属性显示给用户,值将在表单提交时发送到服务器。

You so you need to create an array in the given format from the array received from redux store.因此,您需要从从 redux 存储接收的数组中创建给定格式的数组。

...
render(){
    const { technology } = this.props;
    const rsOptions = technology.map(x => {label: x, value: x});
    return (
        <div className="col-md-4 d-flex align-items-center" style={borderClass}>
            <label className="mb-0 font-weight-bold" style={labelFont}>Technology</label>
                <Select
                    styles={styles}
                    options={rsOptions}
                    defaultValue={{label: 'abcd', value: 'abcd'}}
                    // value={{label: 'abcd', value: 'abcd'}}  // use value instead of defaultValue if its an controlled input
                    // you can also use an object from your array by index
                    // defaultValue={rsOptions[0]}
                    // or you can use find in your array
                    // defaultValue={rsOptions.find(x => x.value === 'abcd)}
                    placeholder="None Selected"
                />
          </div>
    );
}
...

You must re-map your array of string to an array of object, react-select accept this format for options prop.您必须将字符串数组重新映射到对象数组,react-select 接受此格式的选项道具。

[{ value: 'your value', label: 'your label' }]

<Select options={this.props.technology.map(t => ({ value: t, label: t})) } />

Checkout the official docs here !这里查看官方文档!

you need to use map to traverse the array and render a option for您需要使用 map 来遍历数组并呈现一个选项
render each piece of the array.渲染数组的每一部分。

<select className="form-control" value={currentValue} onChange={onChange}>
            <option value="">
                Select one...
            </option>
            {array.map(text,i => (
                <option key={i} value={text}>
                    {text}
                </option>
            ))}
        </select>

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

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