question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot select option from extremely simple list (codesandbox available)

See original GitHub issue

Why in the world doesn’t this work? https://codesandbox.io/s/qzz927y0w9

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

export default class SingleSelect extends Component {
  state = { selectedOption: "blue" };

  render() {
    const options = ["red", "blue", "green"];
    console.log("selected option is", this.state.selectedOption);
    return (
      <Select
        options={options}
        value={this.state.selectedOption}
        onChange={selectedOption => this.setState({ selectedOption })}
        getOptionLabel={option => option}
        getOptionValue={option => option}
      />
    );
  }
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:9
  • Comments:7

github_iconTop GitHub Comments

11reactions
sarinkcommented, Nov 19, 2018

One more annoyance with this “you must convert options to objects” is that if you’re using a type system, you have to routinely cast between the “option-version” and the “normal-version” of your objects. Eg, If I have my colors declared as an enum, I have to also create a “color-option” type, store ColorEnums in the component state, convert them to ColorOption in render, then cast them inside my onChange handler and my convertColorToOption function. It ends up being quite a bit of code where the end result is simply… a dropdown list.

Building a simple dropdown list of strings seems like a very common use case to me, and imo the library should address this in a more convenient manner.

I’d love to see something like this Just Work:

return (
      <Select
        options={options}
        value={this.state.selectedOption}
        onChange={selectedOption => this.setState({ selectedOption })}
      />
);
2reactions
Rall3ncommented, Nov 13, 2018

@sarink

This does not work because of the function cleanValue from utils.js. It expects the value to be an object.

You could use a wrapper function around your values to convert them to objects and update state with the value of the object.

const arrToOption = opt => ({ value: opt, label: opt });

export default class SingleSelect extends Component {
  state = { selectedOption: "blue" };

  render() {
    const options = ["red", "blue", "green"];
    
    return (
      <Select
        options={options.map(arrToOption)}
        value={arrToOption(this.state.selectedOption)}
        onChange={selectedOption =>
          this.setState({ selectedOption: selectedOption.value })
        }
      />
    );
  }
}

CodeSandbox

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot get the selected value in React-select - Stack Overflow
In case of value option is computed as undefined then defaultValue will be picked by react-select. For that try setting the state as...
Read more >
select the dropdown option then they remove ... - CodeSandbox
select the dropdown option then they remove from option and enter into list. 0. Embed Fork Create Sandbox Sign in. Sandbox Info. select...
Read more >
React key attribute: best practices for performant lists
Looking into how React "key" attribute works, how to use it correctly, how to improve performance of lists with it, and why array...
Read more >
chakra-react-select - npm
Start using chakra-react-select in your project by running `npm i ... You can view the whole list of available color schemes in the...
Read more >
Get Started | React Hook Form - Simple React forms validation
One of the key concepts in React Hook Form is to register your component into the hook. This will make its value available...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found