Cannot select option from extremely simple list (codesandbox available)
See original GitHub issueWhy 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:
- Created 5 years ago
- Reactions:9
- Comments:7
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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, storeColorEnum
s in the component state, convert them toColorOption
in render, then cast them inside myonChange
handler and myconvertColorToOption
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:
@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.
CodeSandbox