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.

[V2] Setting initial value of select component with a string value no longer works

See original GitHub issue

Maybe I’m missing something, but from what I can tell the way that used to work to set the initial value of the select component does not work anymore in version 2.0. Here’s what used to work (simplified for readability):

<ReactSelect
    value={field.value}
    options={field.options.map((option) => ({
        ...option,
        label: option.name,
        value: option.name,
        clearableValue: true
    }))}
/>

In the above example the field.value was a string value coming back from the server that matched the name of one of the options.

Is it no longer possible to set the initial value (and all subsequent values) with a string? That was a very nice feature and I would like to make a request for it to make a comeback if it’s no longer available. If it’s still there, can someone show me the light? I can’t seem to get it working with v2.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:9
  • Comments:23 (5 by maintainers)

github_iconTop GitHub Comments

7reactions
ghostcommented, Sep 11, 2018

Just spent two hours debugging just to realize this was the issue. This is not even mentioned in the upgrade guide! I stumbled upon this only when I saw this type ValueType = OptionType | OptionsType | null | void in the reference.

5reactions
elboletairecommented, Oct 5, 2018

I don’t understand why do you need an additional wrapper. Having an array with objects having label and value keys can be easily returned as string with the current version 2, without any additional component:

const options = [
  {label: 'Whatever', value: 'whatever'},
  {label: 'Two', value: 2},
]
return (
  <ReactSelect
    isMulti={false}
    options={options}
    value={options.filter(({value}) => value === this.state.value)}
    getOptionLabel={({label}) => label}
    getOptionValue={({value}) => value}
    onChange={({value}) => this.setState({value})}
  />

Doing it this way, in this.state.value we’ll have the string (or number, or whichever value you have selected), instead of the entire object.

Edit: Please note that I’ve used label and value, as it’s the default used by react-select, and also it’s the one exemplified in the package linked by @jossmac. But you can use this approach for any kind of array if I’m not wrong.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Setting a default value with react-select not working
I have a simple React component which gets an ...
Read more >
How to set default value in select using ReactJS
Following are the two approaches to set default value in select using ReactJS: Approach 1: Without using React Select component.
Read more >
Form Input Bindings - Vue.js
v-model will ignore the initial value , checked or selected attributes found on any form elements. It will always treat the current bound...
Read more >
ASP.NET Core Blazor data binding - Microsoft Learn
There's no sensible way to represent a <select> element option value as a C# object null value, because: HTML attributes can't have null...
Read more >
Form Select | Components - BootstrapVue
options can be an array of strings or objects, or a key-value object. ... Only basic/native HTML is supported in the html field...
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