7

I making a select option using react-select following this Installation and usage example code.

There was an object of array to store the option like this:

const options = [
  { value: 'chocolate', label: 'Chocolate' },
  { value: 'strawberry', label: 'Strawberry' },
  { value: 'vanilla', label: 'Vanilla' }
];

Now, I want to make it dynamically to store the value from other data and make it iterate on the object.

like this:

const options = [
      { value: flavor, label: flavor },
    ];

The flavor on the value and the label are an array. Let's say the array was store the data like this:

flavor = ['chocolate', 'strawberry', 'vanilla']

So, whenever the array added new value, the object of array above also add the value on the iterate.

So, how to making iterate in that..?

or should I thinking to figure out in the component..?

<Select
  value={selectedOption}
  onChange={this.handleChange}
  options={options}
/>

EDIT: the result what I want are the object inside the array is added according to the array values, let's say if we have one data inside array, then the const options store data like this:

const options = [
      { value: flavor, label: flavor },
    ];

then if the array store 2 data, then the const options be like this:

const options = [
      { value: flavor, label: flavor },
      { value: flavor, label: flavor },
    ];

The purpose of this are for make dropdown options on the select have dynamically values.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Tri
  • 2,722
  • 5
  • 36
  • 65

2 Answers2

10

You can use an .map function to dynamically created your options :)

import React, { Component } from 'react';
import { render } from 'react-dom';
import Hello from './Hello';
import './style.css';


const options = [
    { value: 'flavor', label: 'flavor' },
    { value: 'yummy', label: 'yummy' },
    { value: 'red', label: 'red' },
    { value: 'green', label: 'green' },
    { value: 'yellow', label: 'yellow' },
];

class App extends Component {
    state = {
        selectedOption: 'None',
    }

    handleChange = ({ target }) => {
        this.setState({
            selectedOption: target.value,
        });
    }

    render = () => (
        <div>
            <span>{this.state.selectedOption}</span>
            <select
            value={this.state.selectedOption}
            onChange={this.handleChange}
            >
            {options.map(({ value, label }, index) => <option value={value} >{label}</option>)}
            </select>
        </div>
    );
}

render(<App />, document.getElementById('root'));

Working example here https://stackblitz.com/edit/react-select-example

stwilz
  • 2,226
  • 2
  • 21
  • 24
  • 1
    And to your question "or should I thinking to figure out in the component" the answer is yes. If you abstracted that into a component it would be scalable and easy to test. – stwilz Apr 12 '19 at 00:05
  • I have figure out this, thanks for taking your time to answers my question :). – Tri Apr 12 '19 at 01:14
  • This answers implies the usage of the normal select component, but the question wanted a solution based upon the react-select library. – Rallen Apr 12 '19 at 08:06
4

I have figure out this with following this answer, this is the answer what I want base on my questions:

const options = [];
    obj = {}

    for(var i = 0; i < flavor.length; i++) {
      var obj = {};

      obj['value'] = flavor[i];
      obj['label'] = flavor[i];
      options.push(obj);
    }
Tri
  • 2,722
  • 5
  • 36
  • 65