0

My input data is in Array format like ["A","B","C"] and i would like to display them as options in react-select.

    import React, { Component } from 'react';
import Select from 'react-select';

const test= ["a","b","c"]



class TestSelect extends React.Component {
  state = {
    multi: null,
  };

  handleChange = name => value => {
    this.setState({
      [name]: value,
    });
  };
  render() {
    return (
      <div className='dropdown' style={{ dispay: 'inline-block', width: 250, paddingLeft: 50, paddingTop: 50 }}>
        <Select
          options={test}
          value={this.state.multi}
          autosize={true}
          onChange={this.handleChange('multi')
          isMulti
          placeholder="Select Values"
        />

      </div >
    );
  }
}
export default TestSelect;

Is it possible to use the array as option or it is always should be a object?

2
  • 1
    Only receive object based on the documentation example. const options = [ { value: 'chocolate', label: 'Chocolate' }, { value: 'strawberry', label: 'Strawberry' }, { value: 'vanilla', label: 'Vanilla' } ]; Commented Dec 31, 2018 at 3:53
  • Any sample code which converts the array to object model? Commented Dec 31, 2018 at 14:39

1 Answer 1

3

According to the documentation, the structure of the options must respect the following pattern [{label: 'your label', value: 'your value'}, ...]

You will need to convert your array like this:

const options = test.map(v => ({
  label: v,
  value: v
}));

and then pass this value as your options.

Here a live example of your code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.