1

Using https://github.com/JedWatson/react-select. I have an method that will loop through an array that I would like to input into the value for react-select picker. Any ideas why const value is invalid?

  renderList (thelist, i) {
    const { selectedOption } = this.state;

    console.log(thelist);

    // this throws me error
    const value = {
      value: {thelist.name},
      label: {thelist.name}
    }

   return (
    <div>
     <Select
       name="form-field-name"
       onChange={this.handleChange}
       value={value}
     />
   </div>
);

1 Answer 1

2

You don't need the curly braces for the object values. Just do:

const value = {
  value: thelist.name,
  label: thelist.name
}

You only use curly braces when declaring an object or when you need to tell React to interpret something inside render() as plain JavaScript instead of a string.


However, you probably want to define an options prop too to your select component. value only gives the dropdown a selected value, but options is what actually defines... well, the options. The options can be multiple, so we define them in an array of objects.

So do:

options={[
  {
    value: thelist.name1,
    label: thelist.name1
  },
  {
    value: thelist.name2,
    label: thelist.name2
  }
]}
Sign up to request clarification or add additional context in comments.

3 Comments

@user2545642, no worries. Please consider marking it as accepted if it fully answered your question, when you can.
but hey follow up question if you dont mind. Any ideas of how to list all array items into one select list? This one would create multiple ones with only one item per list.
@user2545642 Like so? :)

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.