How to use a array of strings as a options in the react-select
Asked Answered
S

7

6

I am new to react. I am using react select for the select. Now, I have the following 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.

Smelt answered 4/2, 2019 at 11:27 Comment(0)
P
11

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 } />
Pusillanimous answered 4/2, 2019 at 11:41 Comment(2)
What if I have a single string value like "abcd" and I want it be autoselect with this valueSmelt
You will find what you need in this link : appdividend.com/2018/10/19/…Pusillanimous
C
3
<Select 
options={options} 
getOptionLabel={option => option} 
getOptionValue={option => option}
/>
Cry answered 2/7, 2020 at 6:15 Comment(2)
Doesn't seem to be supported github.com/JedWatson/react-select/issues/…Modification
It doesn't support using array of strings like this. You have to make it object which returns label and value.Moises
L
1

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"
 />
Linguini answered 4/2, 2019 at 11:46 Comment(3)
I have used this sol. but it haS 2200 items. so this thing is getting hangedSmelt
If I have a single string and I want it be autoselect then what is the way to do utSmelt
Hi @Linguini could you please look #55058017Smelt
F
1

React-Select expects an array of objects for options with this format:

[..., { 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.

...
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>
    );
}
...
Foible answered 4/2, 2019 at 13:11 Comment(1)
could you please look at #55058017Smelt
E
0

You must re-map your array of string to an array of object, react-select accept this format for options prop.

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

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

Checkout the official docs here!

Edwin answered 4/2, 2019 at 11:47 Comment(0)
A
0

In React, the most efficient way would be to use the useMemo hook to map over the array of strings.

const options = useMemo(() => {
    return stringArray.map((option) => ({
      value: option,
      label: option,
    }));
  }, []);
Austinaustina answered 25/4 at 13:9 Comment(0)
T
-1

you need to use map to traverse the array and render a option for
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>
Trochophore answered 4/2, 2019 at 11:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.